Need an error handler. Advice please

hellfire45

Active Member
Joined
Jun 7, 2014
Messages
464
I highlighted the row that is giving me the error. This file I am opening (docsource1), sometimes it has a specific worksheet, "Regions Messages", and sometimes it doesnt. I need some kind of error handler so that when the sheet DOESN'T exist, It will skip that section with regions messages and move on with the rest of the program.

Any suggestions on how I can write that into the code? Thank you so much!

Code:
If Dir(filepath1 & docname1) <> "" Then
    docsource1 = "VS_Workload-" & Format(Date, "yyyy-mm-dd") & ".xls"
    
ElseIf Dir(filepath1 & docname1a) <> "" Then
    docsource1 = "VS_Workload-" & Format(Date - 1, "yyyy-mm-dd") & ".xls"
    
ElseIf Dir(filepath1 & docname1b) <> "" Then
    docsource1 = "VS_Workload-" & Format(Date - 2, "yyyy-mm-dd") & ".xls"
    
ElseIf Dir(filepath1 & docname1c) <> "" Then
    docsource1 = "VS_Workload-" & Format(Date - 3, "yyyy-mm-dd") & ".xls"
    
ElseIf Dir(filepath1 & docname1d) <> "" Then
    docsource1 = "VS_Workload-" & Format(Date - 4, "yyyy-mm-dd") & ".xls"
End If


Workbooks.Open filepath1 & docsource1, ReadOnly:=True

[B]Workbooks(docsource1).Worksheets("Regions Messages").Activate[/B]Workbooks(docsource1).Worksheets("Regions Messages").Range("A2:M2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy


ThisWorkbook.Sheets("RegionsMessages").Cells(1, 1).SpecialCells(xlCellTypeVisible).PasteSpecial xlValues
 ThisWorkbook.Sheets("RegionsMessages").Activate
 ActiveSheet.Calculate

'this dumps the information that is stored in the "copy"
Application.CutCopyMode = False


'closes the document with the variable name docname
Workbooks(docsource1).Close False


'makes sure that no display alert popups occur
Application.DisplayAlerts = False
 
try something like this:
Code:
Workbooks.Open filepath1 & docsource1, ReadOnly:=True


on error resume next
Workbooks(docsource1).Worksheets("Regions Messages").Activate
on error goto 0
if activesheet.name ="Regions Messages" then
    Workbooks(docsource1).Worksheets("Regions Messages").Range("A2:M2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy




    ThisWorkbook.Sheets("RegionsMessages").Cells(1, 1).SpecialCells(xlCellTypeVisible).PasteSpecial xlValues
    ThisWorkbook.Sheets("RegionsMessages").Activate
    ActiveSheet.Calculate
end if


'this dumps the information that is stored in the "copy"
Application.CutCopyMode = False




'closes the document with the variable name docname
Workbooks(docsource1).Close False
 
Upvote 0

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