gpt4 book ai didi

VBA VLookup 允许用户选择目录上的文件名

转载 作者:行者123 更新时间:2023-12-04 22:09:32 24 4
gpt4 key购买 nike

我一直在尝试插入 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 列。

最佳答案

GetOpenFileName不会返回一个对象。因此,您不能 Set值(value)。

尝试:

FileName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file") 
If FileName = False Then
' User pressed Cancel
MsgBox "Please select a file"
Exit Sub
Else
Workbooks.Open Filename:=FileName
End If

关于VBA VLookup 允许用户选择目录上的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11544930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com