我正在尝试设置工作簿变量" Detailwb"作为用户使用Application.FileDialog(msoFileDialogFilePicker)函数选择的工作簿,但我收到以下错误信息:"运行时错误' 9':下标超出范围"在下面的代码的最后一行。我知道工作簿调用应采用这种格式;工作簿("文件名")但我希望程序调用用户选择的任何.xlsx文件。我只是不确定正确的语法/方法是什么来实现这一目标。
这是一段摘录;
Private Sub CommandButton1_Click()
'Define All Variables
Dim GCell As Range
Dim fDialog As FileDialog, result As Integer
Dim Detailwb As Workbook
Dim Timewb As Workbook
Dim MyDetailReport As String
Dim MyTimeSheet As String
Dim MySheet As String
Dim ProjNum As String
Dim PhaseCode As String
Dim Hours As String
Dim WkEndDate As String
Dim FindRowNumber As Long
Dim FindColumn As Long
'Define Workbooks
Set Timewb = ThisWorkbook
'Find source file
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
fDialog.AllowMultiSelect = False
fDialog.Title = "Select Daily Report File to Import"
fDialog.InitialFileName = "W:\PDX - Mechanical Construction\Operations\Detailing\Daily Tracking Logs\"
fDialog.Filters.Clear
fDialog.Filters.Add "Excel files", "*.xlsx"
If fDialog.Show = -1 Then
MyDetailReport = fDialog.SelectedItems(1)
End If
Workbooks.Open Filename:=MyDetailReport & MyTimeSheet
Set Detailwb = Workbooks(MyDetailReport)
答案 0 :(得分:0)
Workbooks.Open返回对已打开工作簿的引用,因此您只需要一行:
Set Detailwb = Workbooks.Open(Filename:=MyDetailReport & MyTimeSheet)
MyTimeSheet
做了什么?