Optimizing macro for formatting different ranges

tjdickinson

Board Regular
Joined
Jun 26, 2021
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have a relatively long macro which I am trying to optimize for efficiency. In a number of places, there is code such as:
VBA Code:
    ' Format Name header
        If ActiveSheet.Name <> "Toezicht" Then
            With Range("A1")
                .Interior.ThemeColor = xlThemeColorDark2
                .Font.Name = nameHeaderFont
                .Font.Size = nameHeaderSize
                .Font.ThemeColor = xlThemeColorLight2
                .RowHeight = nameHeaderHeight
            End With
        Else
            With Range("A1, A6, A11, A16")
                .Interior.ThemeColor = xlThemeColorDark2
                .Font.Name = nameHeaderFont
                .Font.Size = nameHeaderSize
                .Font.ThemeColor = xlThemeColorLight2
                .RowHeight = nameHeaderHeight
            End With
        End If
which applies the same format to different ranges depending on the Sheet name. I feel like this could be optimized both visually and for the running of the code, but I'm not sure how to do it.

Thanks for any help you can offer!
 

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.
How about
VBA Code:
   Dim Rng As Range
    ' Format Name header
      If ActiveSheet.Name <> "Toezicht" Then Set Rng = Range("A1") Else Set Rng = Range("A1,A6,A11,A16")
      With Rng
          .Interior.ThemeColor = xlThemeColorDark2
          .Font.Name = nameHeaderFont
          .Font.Size = nameHeaderSize
          .Font.ThemeColor = xlThemeColorLight2
          .RowHeight = nameHeaderHeight
      End With
 
Upvote 0
Solution
How about
VBA Code:
   Dim Rng As Range
    ' Format Name header
      If ActiveSheet.Name <> "Toezicht" Then Set Rng = Range("A1") Else Set Rng = Range("A1,A6,A11,A16")
      With Rng
          .Interior.ThemeColor = xlThemeColorDark2
          .Font.Name = nameHeaderFont
          .Font.Size = nameHeaderSize
          .Font.ThemeColor = xlThemeColorLight2
          .RowHeight = nameHeaderHeight
      End With
Sorry for the delayed reaction--I got sidetracked while implementing it throughout the code. It works great. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,869
Messages
6,181,485
Members
453,046
Latest member
Excelvbaexpert

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