我使用 FilePicker 将工作簿定义为变量。在顶部,我将变量定义为公共(public)和字符串。我试图将其声明为工作簿,但它不起作用。当我尝试在另一个模块中进入此工作簿时,将其定义为字符串,我得到
以下编译错误:
Invalid qualifier.
有什么建议有什么问题吗?
Public wipreport As String
sub wip()
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
.Title = "Select WIP Report"
.ButtonName = "OK"
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
wipreport = .SelectedItems.Item(1)
End With
end sub
另一个模块中的代码行,我得到这个编译错误:
wipreport.Worksheets("1. WIP report").Select
谢谢你的帮助。
最佳答案
编译错误是您可能遇到的最好的错误 - 编译器明确告诉它不起作用。在这种情况下,wipreport.Worksheets("1. WIP report").Select
无法工作,因为 wipreport
是 String
类型它没有 Worksheets
属性(property)。
可能尝试这样的事情,如果 wipreport
它将起作用是打开的 Excel 文件的名称:
Workbooks(wipreport).Worksheets("1. WIP report").Select
如果
wipreport
是文件的路径,然后打开它是一个更好的选择:Workbooks.Open wipreport
甚至,为它设置一个变量:
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:=wipreport)
在这个主题上,这个问题在 StackOverflow 的#VBA 中获得最高票数是有原因的 - How to avoid using Select in Excel VBA .
关于excel - 在 VBA 中使用文件选择器打开工作簿会引发编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58289550/