簡體   English   中英

Excel VBA,將變量與VLOOKUp一起使用

[英]Excel VBA, using variables with VLOOKUp

我試圖將VLOOKUP添加到列中,以將數據從一張紙輸入到另一張紙。 查找表上表數組的大小會有所不同,因此我想向公式添加變量。

這是我到目前為止的內容:

Sheets("Page1_5").Select

Dim CountryRow As Long
Cells(5, 1).Select
Selection.End(xlDown).Select
CountryRow = ActiveCell.Row
Sheets("Results").Select
Range("B2").Select
 ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Page1_5!$A$5:$F$" & CountryRow & ",6,FALSE)"

任何幫助將不勝感激

您應該堅持並遵循一種方法-開始使用Cells(5, 1).Select ,之后使用Range("B2").Select並最后在VLookup中使用RC[-1]$A$5:$F$" & CountryRow

另外,不需要使用SelectSelectionActiveCell ,而是可以使用完全限定的工作表和范圍。

請嘗試以下代碼:

Option Explicit

Sub TestVlookup()

Dim CountryRow As Long

With Sheets("Page1_5")
    CountryRow = .Range("A5").End(xlDown).Row
End With

With Sheets("Results")
    Range("B2").Formula = "=VLOOKUP(A2,Page1_5!$A$5:$F$" & CountryRow & ",6,FALSE)"
End With

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM