vba - Excel 2016 宏无法找到文件:运行时错误 1004
问题描述
尝试在 Excel 2016 中使用 VBA 打开其他 Excel 文件时遇到问题。文件是否在同一目录中并不重要。我认为这与 Excel 2016 中阻止搜索的默认设置有关吗?宏在 Excel 2010 中运行。
Private Sub CommmandButton_Click()
Dim source As String
Dim temp As Workbook
source = InputBox("Enter source")
Set temp = Workbooks.Open(source)
end sub
解决方案
FileDialog
这是使用对象的示例解决方案
Private Sub CommmandButton_Click()
Dim fDialog As FileDialog, _
wb As Excel.Workbook
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Select a file"
.InitialFileName = "C:\"
.Filters.Clear
' Prevent Error by specifying that the user must use an excel file
.Filters.Add "Excel files", "*.xlsx,*.xls,*.xlsm"
End With
If fDialog.Show = -1 Then
Set wb = Excel.Workbooks.Open(fDialog.SelectedItems(1))
Else
End ' Cleanly exit the Macro if the user cancels
End If
End Sub
推荐阅读
- ruby-on-rails - habtm 中的 Graphql-ruby 范围,嵌套查询中的数据泄漏
- python - PIL 没有 ImageTk 属性
- reactjs - 在 Contentful CMS 中嵌入代码时出现问题
- css - 具有固定选项卡标题和内容中固定 div 的 Angular 材质选项卡
- visual-studio - 如何将主摄像机设置为 Unity 中的其他摄像机位置之一?
- android - 从片段向数据库中插入值时ListView不更新
- firebase - firebase中的方案设计
- javascript - Javascript中的血型
- haskell - 在 Haskell 中细分列表?(不导入Data.List.Split)
- android - 用户登录时的空对象引用