VBA: Combine Macro

datastudent

Board Regular
Joined
Sep 7, 2021
Messages
72
Office Version
  1. 365
Platform
  1. Windows
Hi,

How do I combine all my Macro so that I would only need to click one time? Please note that my Macro are all just reconstruction of data in multiple sheets. I have Sheet1, Sheet2, Sheet3 and Sheet4.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You can call Macros from other Macros. Many people like to break them apart into smaller procedures, to keep it shorter/cleaner, or to make certain parts easy to re-use.

So, if you have Macro1, Macro2, Macro3, you could call them one after the other like this:

VBA Code:
Sub MainMacro()
    Macro1
    Macro2
    Macro3
End Sub
 
Upvote 0
You can call Macros from other Macros. Many people like to break them apart into smaller procedures, to keep it shorter/cleaner, or to make certain parts easy to re-use.

So, if you have Macro1, Macro2, Macro3, you could call them one after the other like this:

VBA Code:
Sub MainMacro()
    Macro1
    Macro2
    Macro3
End Sub
Call was my first thought too and I did it but for some reason its not doing what I expected that's why I asked here maybe there's another way or maybe I'm doing something wrong?

Macro Assignment
Sheet1 - Macro1
Sheet2 - Macro2
Sheet3 - Macro3
Sheet4 - Macro4

So here's what I'm doing, I'm in Sheet1 then I click my Call Macro. What's happening is that all the command in all the Macro is happening in Sheet1 only or in the sheet that I'm in.
 
Upvote 0
If you do not have code at the top of each procedure telling it what sheet to run against, it will default to run against what ever the Active Sheet is at the time that Macro is called.

Also, if you have your code within the Sheet modules, I would recommend creating a General Module, and moving all your procedures there, so they are accessible from every sheet.
 
Upvote 0
Perhaps you can post the code from one of your macros so we can get an idea of what you are trying to so.
 
Upvote 0
If you do not have code at the top of each procedure telling it what sheet to run against, it will default to run against what ever the Active Sheet is at the time that Macro is called.

Also, if you have your code within the Sheet modules, I would recommend creating a General Module, and moving all your procedures there, so they are accessible from every sheet.
I try this one but the code still runs on the sheet where I'm in and not on Sheet4.

Sub Macro4()
'
Dim ws As Worksheet

Set ws = Sheets("Sheet4")
With ws
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A2").Select
ActiveCell.FormulaR1C1 = "=UPPER(RC[1])"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A20")
Range("A2:A20").Select

End With

End Sub
 
Upvote 0
That is not how you write a With statement.
All your ranges within your With need to start with a period (.) to be applied to the With range.
Overwise, it reverts to the Active Sheet again!

Try:
VBA Code:
Sub Macro4()
'
Dim ws As Worksheet

Set ws = Sheets("Sheet4")
With ws
    .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    .Range("A2").FormulaR1C1 = "=UPPER(RC[1])"
    .Range("A2").AutoFill Destination:=.Range("A2:A20")
End With

End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub Macro4()
    Dim ws As Worksheet
    Set ws = Sheets("Sheet4")
    With ws
        .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range("A2").FormulaR1C1 = "=UPPER(RC[1])"
        .Range("A2").AutoFill Destination:=Range("A2:A20")
    End With
End Sub
 
Upvote 0
Mumps, shouldn't that last line in the With block look like this:
VBA Code:
        .Range("A2").AutoFill Destination:=.Range("A2:A20")
instead of this:
VBA Code:
        .Range("A2").AutoFill Destination:=Range("A2:A20")
like I showed in my code (note the period in front of the second Range reference)?
 
Upvote 0
It sure does. Thanks for picking up on that. :)
 
Upvote 0

Forum statistics

Threads
1,226,466
Messages
6,191,196
Members
453,646
Latest member
SteenP

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