我在If headers(iheaders, 1) = SR(1, iSR) Then
遇到错误,第一个循环工作正常。然后第二个循环,我得到subscript out of range
。但是我注意到它总是循环回到For iSR = 1 To UBound(SR, 2)
而不是For iheaders = 1 To UBound(headers, 2)
。
代码的作用:
工作表A上的标题应该与工作表B上的标题匹配。它们都是水平标题。
如果存在匹配项,则将SR的第5行分配给我的变量R
。
代码:
Const FirstMatch As Boolean = True
Dim lastrow As Long
Dim SR As Variant
Dim iSR As Integer
Dim R As Variant
Dim headers As Variant
Dim iheaders As Integer
SR = Worksheets("Sheet A").Range("D3:J7").Value
headers = Worksheets("Sheet B").Range("B1:H1").Value
With Worksheets("Sheet B")
ReDim R(1 To UBound(SR), 1 To 1)
For iheaders = 1 To UBound(headers, 2)
For iSR = 1 To UBound(SR, 2)
If headers(iheaders, 1) = SR(1, iSR) Then
R(iSR, 1) = SR(5, iSR)
If FirstMatch Then
Exit For
End If
End If
Next
Next
'Populate R to where I want on Total Page
End With