VBA Help. Select Case based on worksheet name

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
I have three different macros that make various format adjustments. 1) intacct_CMA 2) intacct_events 3) Intacct

I'm trying to consolidate the three macros into one by using a Select Case Statement based on the Worksheet names.
I would like for:
- macro #1 to run on the worksheet named "Detailed P&L - Unaudited - CMA"
- macro #2 to run on P&L - General, P&L - Industry Initiatives, P&L - Christmas, P&L - Awards Show, and P&L - Music Fest
- macro #3 on all other sheets

The code below loops through the workbook but does not call the formatting macros based on the worksheet names.

Any suggestions on how to modify?


Code:
Dim ws As Worksheet    For Each ws In Worksheets
        Select Case UCase(ws.Name)
           Case "Detailed P&L - Unaudited - CMA "
                With ws
                    Call intacct_CMA
                End With
            Case "P&L - General", "P&L - Industry Initiatives", "P&L - Christmas", "P&L - Awards Show", "P&L - Music Fest"
                With ws
                    Call intacct_events
                End With
           Case Else
                With ws
                    Call Intacct
                End With
     End Select
    Next ws
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
try adding the line in red

Code:
For Each ws In Worksheets
 [color=red]ws.Activate[/color]
    Select Case UCase(ws.Name)
           Case "Detailed P&L - Unaudited - CMA "
                With ws
                    Call intacct_CMA
                End With
            Case "P&L - General", "P&L - Industry Initiatives", "P&L - Christmas", "P&L - Awards Show", "P&L - Music Fest"
                With ws
                    Call intacct_events
                End With
           Case Else
                With ws
                    Call Intacct
                End With
     End Select
 
Last edited:
Upvote 0
You are also specifying Upper Case
Code:
UCase(ws.Name)

When your sheet names are not ...try removing that line !
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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