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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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