vlookup在VBA投掷运行时错误1004

时间:2017-08-30 00:38:14

标签: excel-vba vba excel

我是VBA的初学者。我有两张纸,其中常见故障是两列D列中的CTPName。 Sheet'摘要T'分别在E,F,G和H列中得到Var1,Var2,Var3和Var4的输出。 Sheet'Summary T-​​1'在相同的列中输出PrevVar1,PrevVar2,PrevVar3和PrevVar4。我的目标是从第一张表中查找每个CTPName,并从第二张表中获取与其对应的值PrevVar1,PrevVar2,PrevVar3和PrevVar4。在下面的代码中,尝试vvupup为PrevVar1(i)的最后一行引发运行时错误。每张表的总行数约为6000

Dim row1 As Integer
Dim row2 As Integer
Dim i As Integer
Dim CTPName(1 To 10000) As String
Dim Var1(1 To 10000) As Long
Dim Var2(1 To 10000) As Long
Dim Var3(1 To 10000) As Long
Dim Var4(1 To 10000) As Long
Dim PrevVar1(1 To 10000) As Long
Dim PrevVar2(1 To 10000) As Long
Dim PrevVar3(1 To 10000) As Long
Dim PrevVar4(1 To 10000) As Long

row1 = Worksheets("Summary T").UsedRange.Rows.Count
row2 = Worksheets("Summary T-1").UsedRange.Rows.Count

Worksheets("Summary T").Activate

'On Error Resume Next

For i = 1 To row2 - 1
  CTPName(i) = Range("D1").Offset(i)
  Var1(i) = Range("E1").Offset(i)
  Var2(i) = Range("F1").Offset(i)
  Var3(i) = Range("G1").Offset(i)
  Var4(i) = Range("H1").Offset(i)

  PrevVar1(i) = Application.WorksheetFunction.VLookup(CTPName(i), Sheets("Summary T-1").Range("D2:H"), 2, False)

0 个答案:

没有答案