我正在尝试创建一个函数,该函数将滚动工作表中的每一行,并将某个列的值(W)与工作簿中所有其他工作表中的列(始终为T)进行比较。如果值匹配,则该行在其他工作表中具有相应的行。
要添加一些上下文,目标是标记主工作表中其他工作表中没有相应行的任何行。到目前为止,我有这个:
For x = 1 To ThisWorkbook.Sheets.Count
If Sheets(x).Name <> "VAT Transaction Report" Then
Dim Search(x) As Range
Dim LastRow As Long
Set Search(x) = Sheets(x).Range(Cells(1, 16), Cells(LastRow, 17))
With Sheets("VAT Transaction Report")
LastRow = .UsedRange.Row - 1 + .UsedRange.Rows.Count
For y = 2 To LastRow
If IsEmpty(.Cells(y, 1).Value = False) And IsEmpty(.Cells(y,24).Value = True) Then
.Cells(y, 24).Value = Application.VLookup(.Cells(y, 24), Search(x), 2, False)
End If
Next y
End With
End If
Next x
此代码不起作用,因为我的范围Search(x)必须是常量,但我想在工作簿中为每个工作表设置一个范围,无论如何都要这样做吗?
编辑: 以下建议我的代码现在是
Sub search()
Dim search() As Range, x As Integer, v As Integer
v = ThisWorkbook.Sheets.Count
For x = 1 To v
If Sheets(x).Name <> "VAT Transaction Report" Then
ReDim search(x)
Dim LastRow As Long
* Set search(x) = Sheets(x).Range(Cells(1, 16), Cells(LastRow, 17)) *
With Sheets("VAT Transaction Report")
LastRow = .UsedRange.Row - 1 + .UsedRange.Rows.Count
For y = 2 To LastRow
If IsEmpty(.Cells(y, 1) = False) And IsEmpty(.Cells(y, 24) = True) Then
.Cells(y, 24).Value = Application.VLookup(.Cells(y, 24), search(x), 2, False)
End If
Next y
End With
End If
Next x
With Sheets("VAT Transaction Report")
LastRow = .UsedRange.Row - 1 + .UsedRange.Rows.Count
For y = 2 To LastRow
If IsEmpty(.Cells(y, 1) = False) And IsEmpty(.Cells(y, 24) = True) Then
.Cells(y, 1).Interior.ColorIndex = 3
End If
Next y
End With
End Sub
然而,这会在*标记的行
上抛出应用程序或对象定义的错误