シナリオ:ユーザーから、.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