Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA EXCEL根据3个输入查找2个单元格_Excel_Vba - Fatal编程技术网

使用VBA EXCEL根据3个输入查找2个单元格

使用VBA EXCEL根据3个输入查找2个单元格,excel,vba,Excel,Vba,与标题一样,我应该根据以下3个输入找到2个单元格: 1) 月份(一月、二月、三月ecc.) 2) 初始编号:(1、2、3、4、5、6、7、8) 3) 最终数字:(5,6,7,8,9,10) 为了清楚起见,我还将我的Excel文件发布在下面: 因此,就像在IMG中一样,在给定输入(2月2日、8日)的情况下,如何查找行N5和T5 这是我尝试过的,但它不能正常工作 Set Found = ws.Range("C5:V5").Find(ComboBox1.Value) ' February With

与标题一样,我应该根据以下3个输入找到2个单元格:

1) 月份(一月、二月、三月ecc.) 2) 初始编号:(1、2、3、4、5、6、7、8)
3) 最终数字:(5,6,7,8,9,10)

为了清楚起见,我还将我的Excel文件发布在下面:

因此,就像在IMG中一样,在给定输入(2月2日、8日)的情况下,如何查找行N5T5

这是我尝试过的,但它不能正常工作

Set Found = ws.Range("C5:V5").Find(ComboBox1.Value) ' February

With ws.Range(ws.Cells(Found.Column, ComboBox2.Value), ws.Cells(Found.Row, ComboBox3.Value))
    .Clear
    .VerticalAlignment = xlCenter
    .HorizontalAlignment = xlCenter
    .BorderAround ColorIndex:=1, Weight:=xlMedium
End With

谢谢你,祝你好运

假设每个月的名字下面只有10个数字,我相信以下内容将帮助你达到你想要的结果:

Sub FindCells()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with, amend as required

Set Found = ws.Range("C4:V4").Find(ComboBox1.Value) ' February
'find value from ComboBox1 on Range C4:V4

Set FoundInitial = ws.Range(ws.Cells(Found.Row + 1, Found.Column), ws.Cells(Found.Row + 1, Found.Column + 9)).Find(ComboBox2.Value)
'find ComboBox2 value on range below the Month range, assuming there are only 10 numbers per month
Set FoundFinal = ws.Range(ws.Cells(Found.Row + 1, Found.Column), ws.Cells(Found.Row + 1, Found.Column + 9)).Find(ComboBox3.Value)
'find ComboBox3 value on range below the Month range, assuming there are only 10 numbers per month

With ws.Range(FoundInitial.Address & ":" & FoundFinal.Address)
    .Clear
    .VerticalAlignment = xlCenter
    .HorizontalAlignment = xlCenter
    .BorderAround ColorIndex:=1, Weight:=xlMedium
End With
End Sub

你可以这样做。我的月份在第8行,其他数字在第9行

一旦你掌握了它,这可以简化

Sub testing5()


Dim lColMth As Long
Dim rYear As Range
Dim lFirstNum As Long
Dim lSecondNum As Long
Dim rOutput As Range

lColMth = WorksheetFunction.Match("February", Rows(8), 0)

Set rYear = Cells(9, lColMth).Resize(1, 12)

lFirstNum = WorksheetFunction.Match(3, rYear, 0) + lColMth - 1
lSecondNum = WorksheetFunction.Match(9, rYear, 0) + lColMth - 1

Set rOutput = Range(Cells(9, lFirstNum), Cells(9, lSecondNum))
rOutput.Select

End Sub

什么“不起作用”你能发布一个数据示例(不是完整的工作簿)吗?如果出现错误,什么“不起作用”等等。Thanks@Nathan_Sav它没有给我错误,我只是找不到我需要的细胞。在Found.Column=13和ComboBox2.Value=2中,最终结果单元格(13,2)为M2,其值不正确。希望我是清楚的。RegardsUse
匹配
而不是查找<代码>匹配(“二月”,行(“6:6”),0)然后调整范围大小,并使用该范围上的第一个和第二个值执行相同的操作。合并的单元格在VBA中并不真正存在。您的值位于单元格M5中,因为合并的单元格值位于合并区域中最左侧最顶部的单元格中。根据上面的@Nathan_Sav使用调整大小为什么不直接计算?如果它是每月10个数字,一月是第一个,那么它就是
10x(monthnumber-1)+初始数字,这是你要找的第一个哇,这对我来说太复杂了。我能问你一些意见吗?谢谢@Nathan_Savy使用VBA和google中的帮助进行解释,实际上只有一行代码,只是标准不同而已。你看起来只是给了我,我终于做到了,一开始看起来很可怕,但是读得好会让事情变得更容易。内森,你是个很棒的老师@这不是问题。