Displaying sheets with specific column widths.

cthompson

Board Regular
Joined
Jan 31, 2011
Messages
80
Hi All,

I have a macro that displays hidden sheets based on sheet names in a range, and for these sheets it sets the column widths to specific dimensions. This macro runs without problems, but what I am trying to do now is for specific sheet names e.g sheet named "1234" I want to set different column widths.

Below is the macro that i am working with, and at the end of the macro in "red" type is my attempt to identify the one sheet when un-hidden has a different column width than the rest of the sheets. Any suggestions would be appreciated.


Sub Macro1()
'
' Macro1 Macro

Sheets("Table of Contents").Select

Dim s As Worksheet

For Each s In Worksheets
If Not s.Name = "Table of Contents" Then s.Visible = xlSheetHidden
Next s

ActiveWorkbook.Names.Add Name:="Penn", RefersToR1C1:="=Portfolio_Table!R1C16:R100C16"
ActiveWorkbook.Names("Penn").Comment = ""
Dim oneCell


For Each oneCell In Range("Penn")

On Error Resume Next

ActiveWorkbook.Sheets(CStr(oneCell.Value)).Visible = xlSheetVisible

ActiveWorkbook.Sheets(CStr(oneCell.Value)).Visible = xlSheetVisible
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Visible = xlSheetVisible
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("a").ColumnWidth = 22.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("b").ColumnWidth = 40.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("c").ColumnWidth = 11
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("d").ColumnWidth = 15.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("e").ColumnWidth = 30
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("f:g").ColumnWidth = 23.25
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("h:i").ColumnWidth = 13.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("j").ColumnWidth = 19
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("k").ColumnWidth = 21.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("l:v").ColumnWidth = 15.5
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("w:z").ColumnWidth = 20
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("aa").ColumnWidth = 34
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("ab:ac").ColumnWidth = 15.5

On Error GoTo 0
For ActiveWorkbook.Sheets(CStr(oneCell.Value)) = "1234" To ActiveWorkbook.Sheets(CStr(oneCell.Value)) = "1234"
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Visible = xlSheetVisible
ActiveWorkbook.Sheets(CStr(oneCell.Value)).Columns("a:z").ColumnWidth = 10



Next oneCell

Sheets("Table of Contents").Select

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,814
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Maybe:
Code:
Sub Macro1()
    Application.ScreenUpdating = False
    Sheets("Table of Contents").Select
    Dim s As Worksheet
    For Each s In Worksheets
        If Not s.Name = "Table of Contents" Then s.Visible = xlSheetHidden
    Next s
    ActiveWorkbook.Names.Add Name:="Penn", RefersToR1C1:="=Portfolio_Table!R1C16:R100C16"
    ActiveWorkbook.Names("Penn").Comment = ""
    Dim oneCell
    For Each oneCell In Range("Penn")
        If Sheets(CStr(oneCell.Value)).Name = "1234" Then
            With Sheets(CStr(oneCell.Value))
               .Visible = xlSheetVisible
               .Columns("a:z").ColumnWidth = 10
            End With
        Else
            On Error Resume Next
            With Sheets(CStr(oneCell.Value))
                .Visible = xlSheetVisible
                .Columns("a").ColumnWidth = 22.5
                .Columns("b").ColumnWidth = 40.5
                .Columns("c").ColumnWidth = 11
                .Columns("d").ColumnWidth = 15.5
                .Columns("e").ColumnWidth = 30
                .Columns("f:g").ColumnWidth = 23.25
                .Columns("h:i").ColumnWidth = 13.5
                .Columns("j").ColumnWidth = 19
                .Columns("k").ColumnWidth = 21.5
                .Columns("l:v").ColumnWidth = 15.5
                .Columns("w:z").ColumnWidth = 20
                .Columns("aa").ColumnWidth = 34
                .Columns("ab:ac").ColumnWidth = 15.5
            End With
            On Error GoTo 0
        End If
    Next oneCell
    Sheets("Table of Contents").Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,196,007
Messages
6,012,831
Members
441,732
Latest member
Ayon

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
Top