我正在两列之间进行VLOOKUP功能,使用下面的宏:
Sub ExcelJoin()
On Error Resume Next
Dim Dept_Row1 As Long
Dim Dept_Clm1 As Long
Dim LastRowA As Long
Dim LastColO As Long
Set currentsheet = ActiveWorkbook.Sheets(1)
ctr = 0
LastRowA = currentsheet.Range("A" & Rows.Count).End(xlUp).Row
LastRowO = currentsheet.Range("O" & Rows.Count).End(xlUp).Row
Table1 = currentsheet.Range("A2:A" & LastRowA)
Table2 = currentsheet.Range("O2:O" & LastRowO)
Dept_Row1 = currentsheet.Range("B2").Row
Dept_Clm1 = currentsheet.Range("B2").Column
For Each cl In Table1
currentsheet.Cells(Dept_Row1, Dept_Clm1).FormulaR1C1 = "=VLOOKUP(RC[-1], R2C15:R14C15, 1, False)"
Dept_Row1 = Dept_Row1 + 1
ctr = ctr + 1
Next cl
End Sub
但是到目前为止R2C15:R14C15范围内的行数未知,我应该使用LastRowO将R14作为变量。但是我对合成酶有一些问题,因为我不知道如何以正确的方式将这个变量真正地放入VLOOKUP中。
答案 0 :(得分:1)
这个应该有效:
currentsheet.Cells(Dept_Row1, Dept_Clm1).FormulaR1C1 = "=VLOOKUP(RC[-1], R2C15:R" & LastRowO & "C15, 1, False)"
不过,没有必要使用循环For Each cl In Table1
。您可以在一行代码中将公式应用于整个范围。
变化:
For Each cl In Table1
currentsheet.Cells(Dept_Row1, Dept_Clm1).FormulaR1C1 = "=VLOOKUP(RC[-1], R2C15:R14C15, 1, False)"
Dept_Row1 = Dept_Row1 + 1
ctr = ctr + 1
Next cl
到
currentsheet.Range("B2:A" & LastRowA).FormulaR1C1 = "=VLOOKUP(RC[-1], R2C15:R" & LastRowO & "C15, 1, False)"