VBA Column Size

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with this code to make all worksheets of a workbook with columns A, B & C a specific size.

Code:
Sub ColumnPrep()
'

Dim wks           As Worksheet
  
  Application.ScreenUpdating = False
  For Each wks In Worksheets
    wks.Activate
    
    Range("A:A").Activate
    Selection.ColumnWidth = 8
    
    Range("B:B").Activate
    Selection.ColumnWidth = 15
    
    Range("C:C").Activate
    Selection.ColumnWidth = 35
    

  Next wks

   
End Sub

My question is now how do I exclude this code from acting on certain worksheets?

For example, how would I tell this code if the worksheets are named "DllBytes", "Sheet1" and "TOTALS" to skip this code from acting on it?



Thank you,
Pinaceous
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try

Code:
Sub ColumnPrep()
Dim wks As Worksheet
  Application.ScreenUpdating = False
  For Each wks In Worksheets
    If wks.Name <> "DllBytes" And wks.Name <> "Sheet1" And wks.Name <> "TOTALS" Then
    wks.Activate
    Range("A:A").ColumnWidth = 8
    Range("B:B").ColumnWidth = 15
    Range("C:C").ColumnWidth = 35
  End If
  Next wks
End Sub
 
Upvote 0
Hi Michael M.,

That really worked out well! Thank you!

How could I add a specification to one of those sheets?

For example, to the TOTALS worksheet, how would I do something like:

Code:
If wks.Name"TOTALS" Then

Range("M:M").Activate
    Selection.ColumnWidth = 15

Many thanks,
Paul
 
Upvote 0
Code:
Sub ColumnPrep()
Dim wks As Worksheet
  Application.ScreenUpdating = False
  For Each wks In Worksheets
      If wks.Name = "TOTALS" Then
        wks.Activate
        Range("M:M").ColumnWidth = 15
    End If
    If wks.Name <> "DllBytes" And wks.Name <> "Sheet1" And wks.Name <> "TOTALS" Then
    wks.Activate
    Range("A:A").ColumnWidth = 8
    Range("B:B").ColumnWidth = 15
    Range("C:C").ColumnWidth = 35
  End If
  Next wks
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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