执行以下代码
时出现运行时错误13Dim sh, shmem As Worksheet
Dim rw As Range
Set shmem = Sheets("SHEET1")
Set sh = Sheets("SHEET2")
For Each rw In sh.Rows
If sh.Cells(rw.Row, 1).Value = "" And sh.Cells(rw.Row, 2).Value = "" Then
Exit For
End If
With Application.WorksheetFunction
Dim bdaytest As Variant
Dim match1 As Double
bdaytest = .Index((shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)) * (shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)), 0)
'match1 = .Match(1, .Index((shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)) * (shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)), 0), 0)
bdaytest = .Index(1, shmem.Range("D2:D121"), match1)
End With
Next rw
错误发生在我从2行(现在已注释掉)
中提取的以下行中bdaytest = .Index((shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)) * (shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)), 0)
'match1 = .Match(1, .Index((shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)) * (shmem.Range("A2:A121") = sh.Cells(rw.Row, 1)), 0), 0)
我理解错误必定发生,因为 bdaytest 是错误的数据类型,但我不确定,到目前为止我找不到任何解决方案。提前感谢任何建议。
编辑:我想找出2列(A& B)具有请求值的行的行号。请求的值位于sh.Cells(rw.Row, 1)
和sh.Cells(rw.Row, 2)
答案 0 :(得分:4)
与公式不同,您无法像使用=
和*
那样在VBA中创建数组。你能做的就是像这样使用Application.Countifs
:
Dim sh As Worksheet
Dim shmem As Worksheet
Dim rw As Range
Set shmem = Sheets("SHEET1")
Set sh = Sheets("SHEET2")
For Each rw In sh.Rows
If sh.Cells(rw.Row, 1).Value = "" And sh.Cells(rw.Row, 2).Value = "" Then
Exit For
End If
With Application
Dim bdaytest As Variant
Dim match1 As Double
bdaytest = .Match(1, .CountIfs(sh.Cells(rw.Row, 1), shmem.Range("A2:A121"), _
sh.Cells(rw.Row, 2), shmem.Range("B2:B121")), 0)
If Not IsError(bdaytest) Then bdaytest = shmem.Range("D2:D121").Cells(bdaytest)
End With
Next rw
注意:WorksheetFunction.Countifs
无效。