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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,223,931
Messages
6,175,465
Members
452,645
Latest member
Tante

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