Seeking method to widen column width based on column count

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
Using XL 07, cells F1:P1 are populated with the words 'Hide' or 'Show'. The code below simply hides the entire column based on entries in row 1. In addition, I'd like a method to widen each of the 'Show' columns when the macro is run. There are a total of 11 columns. When all 11 are shown, column width should be 8. With 10 shown, column width should be 8.5. With 9 columns shown, width should be 9. For simplicity, let's assume that for each additional hidden column, the width of remaining shown columns should widen by 0.5.

Would appreciate any and all advice.

Thanks
jim


Code:
Sub HideNetGrid()

Application.ScreenUpdating = False

For Each r In Range("F1:P1")

   If r.Value = "Hide" Then
      r.EntireColumn.Hidden = True
   Else
      r.EntireColumn.Hidden = False
   End If

Next

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
Sub HideNetGrid()

Dim counter As Integer

Application.ScreenUpdating = False

For Each r In Range("F1:P1")

   If r.Value = "Hide" Then
      r.EntireColumn.Hidden = True
      counter = counter + 1
   Else
      r.EntireColumn.Hidden = False
   End If

Next

For Each r In Range("F1:P1")
    If Not r.EntireColumn.Hidden Then r.EntireColumn.ColumnWidth = 8 + counter * 0.5
Next r

Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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