场景:用户要求我为他们提供一个按钮,他们可以在其中选择 .xls,它将数据导入表中的相应列。
问题:我将提供下面的代码,但基本上一旦它尝试打开工作簿,它就会给我下面的错误。我已经搜索了许多解决方案,但我仍然收到此错误。
Private Sub Command20_Click()
Dim fdg As FileDialog, vrtSelectedItem As Variant
Dim strSelectedFile As String
Set fdg = Application.FileDialog(msoFileDialogFilePicker)
With fdg
.AllowMultiSelect = False
.ButtonName = "Select"
.InitialView = msoFileDialogViewList
.Title = "Select Input Files"
'add filter for excel
With .Filters
.Clear
.Add "Excel Spreadsheets", "*.xls"
End With
.FilterIndex = 1
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems 'onby be 1
Dim app As New Excel.Application
app.Workbooks.Open (vrtSelectedItem)
app.Worksheets(1).Activate
For Each rwRow In ActiveWorkbook.Worksheets(1).Rows
' Do things with the rwRow object
Next rwRow
strSelectedFile = vrtSelectedItem
Next vrtSelectedItem
Me![txtSelectedFile] = strSelectedFile
Else 'The user pressed Cancel.
End If
End With
Set fd = Nothing
End Sub