现在,我正在使用这种类型的代码来查看O列中的内容,以确定在N列中输入的内容。由于数据是保密的,我只是在这里制作字母和数字。它运行正常,但我没有在VBA代码中对代码进行硬编码,而是希望它引用外部工作簿中的表,因为我将要插入数千个案例和值。
Sub ChangeTest()
Dim LastRow As Long
Dim i As Long
LastRow = Range("O" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Select Case Range("O" & i)
Case "A", "B", "C"
Range("N" & i) = "1"
Case "D","E","F"
Range("N" & i) = "2"
Case "G","H","I"
Range("N" & i) = "3"
End Select
Next i
End Sub
在外部工作簿中,列D将包含案例(A,B,C等),列C将包含要进入第N列的数字(1,2,3等)。原始工作簿。
我不确定现在的代码是否需要稍微编辑以合并参考表,或者它是否需要完全不同。
答案 0 :(得分:0)
为什么不直接运行vlookup而不是运行循环?:
Sub ChangeTest()
Dim LastRow As Long
Dim Referance_Workbook As Workbook
Dim Referance_Tab As Worksheet
Dim Rng As Range
Set Referance_Workbook = Workbooks.Open("[Referance workbook path]")
Set Referance_Tab = Referance_Workbook.Sheets("[Referance tab within referance workbook]")
Referance_Workbook.Close SaveChanges:=False
LastRow = Range("O" & Rows.Count).End(xlUp).Row
Set Rng = Range("N2:N" & LastRow)
Rng.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-11],'[Source Workbook.xlsx]Sheet1'!C1:C2,2,0),""No in refrence table"")"
End Sub
这会查找您参考工作簿/选项卡的A列,并在B列中查找结果