我在P列和Q列中有公司和代码的名称,而我想要获取的是将R列的voookup到P列,如果Value Matches然后将其粘贴到T列。我有这个Voolup代码=“ = VLOOKUP(R2,P:Q,2,FALSE)”,并尝试将其隐藏到VBA。
Dim i As Integer
Dim p, q, r, t As String
Sub esindia()
For i = 2 To 20000 Step 1
p = Sheet1.Range("p2" & i)
q = Sheet1.Range("q2" & i)
r = Sheet1.Range("r2" & i)
t = Sheet1.Range("t2" & i)
If r = p Then
t = r
Sheet1.Range("t" & i) = r
Else
p = r
Sheet1.Range("t" & i) = r
End If
Next
End Sub
我在这里缺少任何东西吗,非常感谢您的帮助。
答案 0 :(得分:0)
With
块很多,可以根据需要将其分解为单个变量以提高可读性。
打破它
ThisWorkbook
Sheets("Sheet1")
Range("T2:T" & LROw)
LRow
= Range("T" & Rows.Count).End(xlUp).Row)
Sub MyVlookup()
With ThisWorkbook.Sheets("Sheet1").Range("T2:T" & Range("T" & Rows.Count).End(xlUp).Row)
.Formula = "=VLOOKUP(R2, P:Q, 2, 0)"
.Value = .Value 'Remove this if you want the formula to show in the sheet
End With
End Sub