2016-12-02 271 views
0

我不断收到这个错误的线Application.WorksheetFunction和阅读这个话题几个小时,我觉得我几乎无处可去。 它与我引用Sheet2的方式有什么关系?或者我不完全了解Application.WorksheetFunction应该做什么?无法获取WorkSheet函数类的match属性 - 语法?

Sub SearchForValues() 

i = 4 'starts the iterator at column D 

Do While Cells(1, i) <> "" 

    Dim l As Long, searchRange As String 
    n = 2 
     Do While Range("A" & n) <> ""  'loop until the last row of data in the first column 
     StartRow = Range("B" & n) 
     EndRow = Range("C" & n) 
     searchRange = "A" & StartRow & ":Q" & EndRow 
     l = Application.WorksheetFunction.Match(Cells(1, i), Worksheets("Sheet2").Range(searchRange), 0) 
     Range("D" & n) = l 
     n = n + 1 
    Loop 

i = i + 1 
Loop 
End Sub 

下面是我拥有的数据的屏幕截图。列B和C都是我想在表1 and each cell across the top is a term I want to search for in that range.

+1

比赛仅适用于一个维数组的例子。无论是一行还是一列。 –

+0

@ScottCraner这样做很有道理,谢谢。我可以使用不同的功能来搜索整个范围吗? – eob

+0

使用VBA Find() –

回答

0

斯科特·克莱纳已经与“
比赛仅适用于一个维数组回答了你的问题在他的评论在Sheet2上,以在搜索每一行的行的范围;或者一行或一列“和”使用VBA查找()“。

这里是你如何使用Range.Find

Sub SearchForValues() 
    Application.ScreenUpdating = False 
    Dim Target As Range 
    Dim x As Long, y As Long 
    With Worksheets("Sheet1") 
     For x = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row 
      For y = 4 To .Cells(1, .Columns.Count).End(xlToLeft).Column 
       Set Target = Worksheets("Sheet2").Range("A" & .Cells(x, "B").Value & ":Q" & .Cells(x, "C").Value) 
       .Cells(x, y).Value = Not Target.Find(.Cells(1, y).Value) Is Nothing 
      Next 
     Next 
    End With 
    Application.ScreenUpdating = True 
End Sub