我一直在尝试VLookup
使用 VBA 将公式插入到列中。这Table_array
是变量并且每个月都在变化,所以我想要一个对话框来指定我要使用的文件。每个月的Table_array
格式都相同,到目前为止我的公式如下:
Sub VlookupMacro()
Dim FirstRow As Long
Dim FinalRow As Long
Dim myValues As Range
Dim myResults As Range
Dim myFile As Range
Dim myCount As Integer
Set myFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
Set myValues = Application.InputBox("Please select the first cell in the column with the values that you're looking for", Type:=8)
Set myResults = Application.InputBox("Please select the first cell where you want your lookup results to start ", Type:=8)
Range(myResults, myResults.Offset(FinalRow - FirstRow)).Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column) & ", myFile.value($A$2:$B$U20000), 5, False)"
If MsgBox("Do you want to convert to values?", vbYesNo) = vbNo Then Exit Sub
Columns(myResults.Column).Copy
Columns(myResults.Column).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
在代码没有越过Set = myFile
行的那一刻,我遇到了“myFile”的问题。任何建议或修改都将受到欢迎。除此之外,变量文件永远不会超过 20000 行(这就是我在公式中固定范围的原因),我们将始终选择第 5 列。