Hi Team,
I am using below code to select previous output file. using filedialog,
it works if user select file, if user cancels it thows an Error. 1004.
how to handle error programmatically and exit. plz assist.
Below is attempted code......
I am using below code to select previous output file. using filedialog,
it works if user select file, if user cancels it thows an Error. 1004.
how to handle error programmatically and exit. plz assist.
Below is attempted code......
VBA Code:
Sub SelectPreviousOutput()
Dim wbk_output As Workbook
strProduct = "Debtor File" ' Passing variable for showing in file name in Title
'if user Cancel getting Error 1004
'Sorry we count find , is it possible moved, Removed, Deleted.
Set wbk_output = Workbooks.Open((SelectFile(strProduct)), False, True) ' 'how to make this line dynamic, if user select cancel it should not throw error.proper exit.
Myend:
On Error Resume Next
wbk_output.Close
Set wbk_output = Nothing
On Error GoTo 0
End Sub
Function SelectFile(ByVal str As String) As String
Dim sFile As String
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Previous weeks - " & str & " Debtor File"
.ButtonName = "Select"
If .Show = -1 Then ' if OK is pressed
sFile = .SelectedItems(1)
Else
SelectFile = ""
MsgBox "You din't Previous output file", vbInformation
'GoTo Myend 'Gets Error Label not defined if activated
Exit Function
End If
End With
End Function