error handling if user chooses wrong file in prompt based on predefined worksheets

carissa7

New Member
Joined
Jan 13, 2018
Messages
8
I have VBA code that prompts a user to select the workbook from a location. I want to add error handling so that if they choose an incorrect file based on the worksheets then apply a message box "wrong file" and re-enable the prompt to choose a new file.

As you can see below I've already set what the existing names of the worksheets should be if they select the correct workbook.


Sub Button4_Click()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim strFileName As String
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim ws1A As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Dim cell As Range
Dim rng As Range
Dim rng2 As Range
Dim RangeName As String
Dim CellName As String
Dim ValueToFind
Dim dstRng As Range
Dim NewFile As Variant

'prompt folder location
NewFile = Application.GetOpenFilename("microsoft excel files (*.xlsm*), *.xlsm*")
If NewFile <> False Then
Set wb1 = Workbooks.Open(NewFile)
'apply error handeling if user does not select a file
Else
MsgBox "No/Wrong File Selected. Program Will Exit."
Application.DisplayAlerts = False
Exit Sub
End If

Set wb2 = ThisWorkbook
Set ws2 = wb2.Sheets("Output")
Set ws1 = wb1.Sheets("RVP Local GAAP")
Set rng = Range("CurrentTaxPerLocalGAAPProvision")
Set rng2 = Range("CurrentTaxPerGroupGAAPProvision")
Set ws1A = wb1.Sheets("RVP Group GAAP")

'Loop through all the values in NamedRange
For Each rng In ws2.Range("NamedRange")
Set dstRng = Nothing
On Error Resume Next
Set dstRng = ws1.Range(rng.Value)
On Error GoTo 0
'Check that the range exists in destination sheet
If Not dstRng Is Nothing Then
'Check that the range exists in the appropriate area
If Not Intersect(dstRng, ws1.Range("CurrentTaxPerLocalGAAPProvision")) Is Nothing Then
'Transfer the value from the next column to the appropriate range in the
'destination sheet
dstRng.Value = rng.Offset(0, 1).Value
Else
End If
End If
Next
For Each rng2 In ws2.Range("NamedRange")
Set dstRng = Nothing
On Error Resume Next
Set dstRng = ws1A.Range(rng2.Value)
On Error GoTo 0
'Check that the range exists in destination sheet
If Not dstRng Is Nothing Then
'Check that the range exists in the appropriate area
If Not Intersect(dstRng, ws1A.Range("CurrentTaxPerGroupGAAPProvision")) Is Nothing Then
''MsgBox "succesful"
''found = False
'Transfer the value from the next column to the appropriate range in the
'destination sheet
dstRng.Value = rng2.Offset(0, 1).Value
Else
End If
End If
Next
MsgBox "Values have copied over sucessfully"

ActiveWindow.FreezePanes = False
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
So U want to make sure that the XL file selected has the correct sheets available? That means that U would have to open the file after its' selection and loop the sheets to check for the correct sheets. If available continue on. If not, close the file and provide message to select another file. Doable but have U considered changing the way U store the files to begin with? HTH. Dave
ps. please use code tags
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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