我在Excel VBA编程中还很新,所以请忍受:)
我有这个脚本,只要A列不为空,它就可以在两张纸上进行VLookup
Dim i As Single
i = 2
Do While Sheet1.Range("A" & i).Value <> " "
Result = Application.VLookup(Sheet1.Range("H" & i), Sheet2.Range("$A$1:$B$30"), 2, False)
Sheet1.Range("B" & i).Value = Result
i = i + 1
Loop
但是我不明白为什么当A列已经为空时,循环仍然继续在B列上放置值。
我错过了停止循环的代码吗?
答案 0 :(得分:0)
Delcare使用其名称定位的工作表(以确保外观中定位的工作表正确)。像这样:
Dim i As Single
Dim TargetSheet as worksheet
Set TargetSheet = Thisworkbook.Sheet("Your sheet name") 'place your sheet name between ""
i = 2
Do While TargetSheet.Range("A" & i).Value <> "" 'make sure the string is empty
Result = Application.VLookup(Sheet1.Range("H" & i), Sheet2.Range("$A$1:$B$30"), 2, False)
Sheet1.Range("B" & i).Value = Result
i = i + 1
Loop