How to Run Macro2 if Macro1 returns ERROR

Bozso

New Member
Joined
Nov 26, 2013
Messages
11
Hello,

I have 2 macros for 2 different situations, and what I would like to achieve is that I do not have to separately run them.
Basically incoming excels files are formatted in 2 different ways. If one of my macros fail, I use the other one.
My aim is that if the first macro returns an error message, then excel should automatically run the second macro for me.

Is this at all possible?

The two macros are as follow:

Macro1:
Code:
Sub Fetch()
    Dim r As Long
    ActiveSheet.Range("D13:D15,D32,D44:D48,D51:D53,D70,D78,D93").Copy
    With Workbooks("input.xls").Worksheets("standard")
        r = .Range("D" & .Rows.Count).End(xlUp).Row
        If .Range("D" & r).Value <> "" Then r = r + 1
        .Range("D" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    End With
    ActiveSheet.Range("C101:C102").Copy
    With Workbooks("input.xls").Worksheets("standard")
        r = .Range("S" & .Rows.Count).End(xlUp).Row
        If .Range("S" & r).Value <> "" Then r = r + 1
        .Range("S" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    End With
End Sub

Macro2:
Code:
Sub Fetchke()
    Dim r As Long
    ActiveSheet.Range("D13:D15,D32,D44:D48,D51:D53,D63,D68,D83").Copy
    With Workbooks("input.xls").Worksheets("standard")
        r = .Range("D" & .Rows.Count).End(xlUp).Row
        If .Range("D" & r).Value <> "" Then r = r + 1
        .Range("D" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    End With
    ActiveSheet.Range("C91:C92").Copy
    With Workbooks("input.xls").Worksheets("standard")
        r = .Range("S" & .Rows.Count).End(xlUp).Row
        If .Range("S" & r).Value <> "" Then r = r + 1
        .Range("S" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    End With   
End Sub


Also suggestions to optimize the existing 2 macros are very welcome :)

Thank You for all your input!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe..

Sub Fetch()
Dim r As Long
On Error GoTo ErrorHandler
ActiveSheet.Range("D13:D15,D32,D44:D48,D51:D53,D70,D78,D93").Copy
With Workbooks("input.xls").Worksheets("standard")
r = .Range("D" & .Rows.Count).End(xlUp).Row
If .Range("D" & r).Value <> "" Then r = r + 1
.Range("D" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
End With
ActiveSheet.Range("C101:C102").Copy
With Workbooks("input.xls").Worksheets("standard")
r = .Range("S" & .Rows.Count).End(xlUp).Row
If .Range("S" & r).Value <> "" Then r = r + 1
.Range("S" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
End With
Exit Sub
ErrorHandler:
Call Fetchke

End Sub
 
Upvote 0
Maybe..

Sub Fetch()
Dim r As Long
On Error GoTo ErrorHandler
ActiveSheet.Range("D13:D15,D32,D44:D48,D51:D53,D70,D78,D93").Copy
With Workbooks("input.xls").Worksheets("standard")
r = .Range("D" & .Rows.Count).End(xlUp).Row
If .Range("D" & r).Value <> "" Then r = r + 1
.Range("D" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
End With
ActiveSheet.Range("C101:C102").Copy
With Workbooks("input.xls").Worksheets("standard")
r = .Range("S" & .Rows.Count).End(xlUp).Row
If .Range("S" & r).Value <> "" Then r = r + 1
.Range("S" & r).PasteSpecial Paste:=xlPasteValues, Transpose:=True
End With
Exit Sub
ErrorHandler:
Call Fetchke

End Sub


It seems this one did the trick! Thank you very much for your help
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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