Excluding a sheet from VBA loop - Column widths

Mr Retirement

New Member
Joined
Nov 12, 2016
Messages
46
Hi All -

I have a code below that works as intended, but I need help changing it slightly.

It currently works to cycle through all worksheets and adjust the column widths from the active sheet.

The change I need is: I want 2 worksheets to be exempt from this editing. The 2 sheets are called "Vis" & "Staff"

Any help would be appreciated!!


Code:
Sub CopyColumnWidths()
On Error GoTo EndIt
Dim wActSht As Worksheet
    Dim arrWidths(101) As Single
    Set wActSht = ActiveSheet
    For x = 3 To 104
        arrWidths(x - 3) = wActSht.Cells(1, x).ColumnWidth
    Next
    For Each sht In Sheets
        If sht.Name <> wActSht.Name Then
            For x = 3 To 104
                sht.Cells(1, x).ColumnWidth = arrWidths(x - 3)
            Next
        End If
    Next
EndIt:
        
End Sub

Thanks guys!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this.
Code:
Option Explicit

Sub CopyColumnWidths()
Dim sht As Worksheet
Dim wActSht As Worksheet
Dim arrWidths(101) As Single
Dim x As Long

    On Error GoTo EndIt

    Set wActSht = ActiveSheet

    For x = 3 To 104
        arrWidths(x - 3) = wActSht.Cells(1, x).ColumnWidth
    Next

    For Each sht In Sheets
        Select Case sht.Name
            Case wActSht.Name, "Viz", "Staff"
                ' do nothing
            Case Else
                For x = 3 To 104
                    sht.Cells(1, x).ColumnWidth = arrWidths(x - 3)
                Next
        End Select
    Next sht
    
EndIt:

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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