Hi,
Can one of you pros take a look at this code and help get the bugs out?
I also attached some screenshots to help.
Compile error: Invalid outside procedure / syntax error
Compile error: Sub or Function not defined
Visual Basic for Applications
Microsoft Access 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
---------------------------------------------------------------------------------------------------------
Code:
Private Sub cmdShow_Click()
On Error GoTo SubError
'Add "Microsoft Office 16.0 Object Library" in references
Dim fdialog As Office.FileDialog
Dim varfgile As Variant
txtSelectedName = ""
' Set up the File Dialog
Set fdialog = Application.FileDialog(msoFileDialogFilePicker)
With fdialog
.Title = "Choose the spreadsheet you would like to import"
.AllowMultiSelect = False
.InitialFileName = "C:\Users\cvlasa1\Downloads\SelfTrade\Export to Excel\" 'Folder picker needs trailing slash
.Filters.Clear
.Filters.Add "Excel files", "*.xls*"
' .Filters.Add "Excel files", "*.xls"
' .Filters.Add "Excel files", "*.xlsx"
' .Filters.Add "Excel macro-enabled", "*.xlsm"
If .Show = True Then
If .SelectedItems.Count = 0 Then
'User clicked open but didn't select a file
GoTo SubExit
End If
'An option for MultiSelect = False
'varFile = .SelectedItems(1)
'txtSelectedName = varFile
'Needed when MultiSelect = True
For Each varFile In .SelectedItems
txtSelectedName = txtSelectedName & varFile & vbCrLf
Next
Else
'user cancelled dialog without choosing!
'Do you need to react?
End If
End With
SubExit:
On Error Resume Next
Set fdialog = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub