vba help - Select file using filedialog - if cancel exit

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
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......

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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
VBA Code:
Sub SelectPreviousOutput()

Dim wbk_output As Workbook
strProduct = "Debtor File"  ' Passing variable for showing in file name in Title

On Error Resume Next
Set wbk_output = Workbooks.Open((SelectFile(strProduct)), False, True) 
If Err.Number <> 0 Then GoTo Myend

'rest of your code goes here

Myend:

    wbk_output.Close
    Set wbk_output = Nothing
On Error GoTo 0

End Sub
 
Upvote 0
Hi Yongle,

Perfect ! it worked. ?
Now I learned how to use err.number in this example. will use it in future. great help (y)



Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top