Macro not working when I "Call" it from another macro, but does work when I select it individually

anarchyflag

New Member
Joined
Nov 2, 2018
Messages
15
Hi,

I have a formatting macro below:

VBA Code:
Sub Colour_whole_sheet()

Dim lastRow As Long
Dim lastColumn As Long

lastRow = Range("A1").End(xlDown).Row
lastColumn = Range("A3").End(xlToRight).Column

'Colour alternate rows purple / white
For Each cell In Range(Cells(1, 1), Cells(lastRow, lastColumn))
    If cell.Row Mod 2 = 1 Then
        cell.Interior.Color = RGB(242, 230, 255)
    Else
        cell.Interior.Color = RGB(255, 255, 255)
    End If
Next cell

End Sub

It doesn't run when I call it from another macro, which is just:

VBA Code:
Sub Run_macros()

[A bunch of other subs]
Call Colour_whole_sheet
[A bunch of other subs]

End Sub

It doesn't come up with an error - it just doesn't do anything. But when I select it specifically on its own, from View > Macros > View Macros > Run, it works fine.

Do you know why this might be?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi anarchyflag. I'm wondering if there's a name clash of some kind. Try renaming "Colour_whole_sheet" to something else, and of course changing the line in "Run_macros" to use the same name, and see if that makes any difference.

Also, do the subs that follow the call to "Colour_whole_sheet" get executed? Or is "Run_macros" shutting down at that point?
 
Upvote 0
Suggest ..
- Put a breakpoint on the Call Colour_whole_sheet line (put your cursor in the line somewhere and press F9
- Run the Run_macros sub

a) If the code halts at the Call Colour_whole_sheet line then step through the code with F8 and observe variable values (hover over them or use the Locals window) as you step through the code. That may turn something up.

b) If the code does not halt at the Call Colour_whole_sheet line then the code must have aborted somewhere in the first lot of [A bunch of other subs]. In this case start again and simply step through the Run_macros code (& the subs it calls) line-by-line with F8 and again something useful might turn up.
 
Upvote 0
Hi both,

Thanks for the suggestions - someone on another forum let me know I needed to declare all the variables as follows:

VBA Code:
Sub Colour_whole_sheet(Optional sht As Variant)

    If IsMissing(sht) Then Set sht = ActiveSheet

    Dim lastRow As Long
    Dim lastColumn As Long
    Dim i As Long

    With sht
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        lastColumn = .Cells(3, .Columns.Count).End(xlToLeft).Column

        'Colour alternate rows purple / white
        With .Range("A1", Cells(lastRow, lastColumn)) 
            .Interior.Color = vbWhite 
            For i = 1 To .Rows.Count Step 2
                .Rows(i).Interior.Color = RGB(242, 230, 255) 
            Next
        End With
    End With

End Sub

Which fixed the issue!
 
Upvote 0
someone on another forum ..
In that case, please take a few moments to read our Forum Rules, particularly #13 for details on the correct way to proceed if posting in more than one forum regarding the same question.
Be sure to follow & read the link at the end of the rule too!
 
Upvote 0
In that case, please take a few moments to read our Forum Rules, particularly #13 for details on the correct way to proceed if posting in more than one forum regarding the same question.
Be sure to follow & read the link at the end of the rule too!

Understood - apologies, I promise that I did read these when I signed up! But it was over a year ago, and I haven't taken the time to reread until now.
 
Upvote 0
Understood - apologies, I promise that I did read these when I signed up! But it was over a year ago, and I haven't taken the time to reread until now.
Cheers. Thanks for your message and taking the time to have another look.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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