How to run macros on a different sheet.

QueensBlvd

New Member
Joined
Nov 8, 2016
Messages
7
I have three sheets with the following titles "Total", "Inpt", and "Outpt".

I've got three individual macros that reformat each sheet. However, they only work when I'm on the sheet I'm reformatting. I would like to create one macro to run all of them from the "Total" tab. I've tried Application.Run with no luck. Does anybody have any ideas? Two of my codes are below. Even just trying to run the Inpt macro on the Outpt tab gives me a Run time error 1004. Thanks!

Code:
Sub Inpt()Dim Inpt As Worksheet
Set Inpt = ThisWorkbook.Sheets("Inpt")


Dim Days As Integer
Days = InputBox("Days in the last 3 months?")




'SECTION ONE'
Dim x As Long
    For x = 2 To Inpt.UsedRange.Columns.Count
        If Columns(x).Hidden = False Then
        'Hide Second Column'
            Columns(x).Hidden = True
        'Insert Third Column'
            Columns(x + 3).EntireColumn.Insert
        'Copy New Month'
            Range(Cells(33, x + 2), Cells(37, x + 2)).Copy
        'Paste New Month'
            Range(Cells(33, x + 3), Cells(37, x + 3)).PasteSpecial
            Range("A1").Select
        'Clear Contents Total AR and Self Pay'
            Range(Cells(35, x + 3), Cells(36, x + 3)).ClearContents
        'New Month Title'
            Cells(3, x + 3) = MonthName(Month(Date))


'SECTION TWO'
        'Copy Columns'
            Range(Cells(3, x + 6), Cells(33, x + 7)).Copy
        'Paste Columns'
            Cells(3, x + 5).PasteSpecial
            Range("A1").Select
        'Update Formulas'
            Range(Cells(4, x + 5), Cells(33, x + 7)).FormulaR1C1 = "=sum(RC[-6]:RC[-4])"
        'Title New Month'
            Cells(3, x + 7) = MonthName(Month(Date))


'SECTION THREE'
        'Copy Columns'
            Range(Cells(3, x + 10), Cells(33, x + 11)).Copy
        'Paste Columns'
            Range("A1").Select
        'Row 35 Formulas'
            Range(Cells(35, x + 9), Cells(35, x + 11)).FormulaR1C1 = "=R[+2]C[-8] / R[-2]C"
            Range("A1").Select
        'New Month Title'
            Cells(3, x + 11) = MonthName(Month(Date))
        'New Month Formula'
            Range(Cells(4, x + 11), Cells(33, x + 11)).FormulaR1C1 = "=RC[-4] / " & Days & ""
        'Clear Contents'
            Range(Cells(32, x + 5), Cells(32, x + 11)).ClearContents
    Exit For
    End If
    Next x
            
        
            
        
             
End Sub




Sub Outpt()
Dim Outpt As Worksheet
Set Outpt = ThisWorkbook.Sheets("Outpt")


Dim Days As Integer
Days = InputBox("Days in the last 3 months?")




'SECTION ONE'
Dim y As Long
    For y = 2 To Outpt.UsedRange.Columns.Count
        If Columns(y).Hidden = False Then
        'Hide Second Column'
            Columns(y).Hidden = True
        'Insert Third Column'
            Columns(y + 3).EntireColumn.Insert
        'Copy New Month'
            Range(Cells(33, y + 2), Cells(37, y + 2)).Copy
        'Paste New Month'
            Range(Cells(33, y + 3), Cells(37, y + 3)).PasteSpecial
            Range("A1").Select
        'Clear Contents Total AR and Self Pay'
            Range(Cells(35, y + 3), Cells(36, y + 3)).ClearContents
        'New Month Title'
            Cells(3, y + 3) = MonthName(Month(Date))


'SECTION TWO'
        'Copy Columns'
            Range(Cells(3, y + 6), Cells(33, y + 7)).Copy
        'Paste Columns'
            Cells(3, y + 5).PasteSpecial
            Range("A1").Select
        'Update Formulas'
            Range(Cells(4, y + 5), Cells(33, y + 7)).FormulaR1C1 = "=sum(RC[-6]:RC[-4])"
        'Title New Month'
            Cells(3, y + 7) = MonthName(Month(Date))


'SECTION THREE'
        'Copy Columns'
            Range(Cells(3, y + 10), Cells(33, y + 11)).Copy
        'Paste Columns'
            Range("A1").Select
        'Row 35 Formulas'
            Range(Cells(35, y + 9), Cells(35, y + 11)).FormulaR1C1 = "=R[+2]C[-8] / R[-2]C"
            Range("A1").Select
        'New Month Title'
            Cells(3, y + 11) = MonthName(Month(Date))
        'New Month Formula'
            Range(Cells(4, y + 11), Cells(33, y + 11)).FormulaR1C1 = "=RC[-4] / " & Days & ""
        'Clear Contents'
            Range(Cells(32, y + 5), Cells(32, y + 11)).ClearContents
    Exit For
    End If
    Next y
            
        
            
        
             
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You need to fully qualify your codes, by that I mean, instead of
Code:
Range(Cells(33, x + 2), Cells(37, x + 2)).Copy

You should do this
Code:
Inpt.Range(Inpt.Cells(33, x + 2), Inpt.Cells(37, x + 2)).Copy
That way it always references cells or ranges on the Inpt sheet even though you are on a different sheet. Do same for the others
 
Upvote 0
Great thanks - I had tried this, but I think I forgot to add the sheet title to the cells value as well.

Thanks!

You need to fully qualify your codes, by that I mean, instead of
Code:
Range(Cells(33, x + 2), Cells(37, x + 2)).Copy

You should do this
Code:
Inpt.Range(Inpt.Cells(33, x + 2), Inpt.Cells(37, x + 2)).Copy
That way it always references cells or ranges on the Inpt sheet even though you are on a different sheet. Do same for the others
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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