Column Width Adjust

rudevincy

Active Member
Joined
Feb 21, 2005
Messages
417
Hello,

I have a worksheet that is populated with data in some columns.

in MyRange is E2:BA2 is a formula that calculates the number of characters in another cell that is based on the column width... so if in that cell(E2) the value is 5 then the column width should be adjusted to 5, but if the value in the cell is 0 then the column width should be 0.

Please I need help with the formula as it is not working right

Sub SizeColumns()

Application.ScreenUpdating = False
Sheets("SheetData").Activate

Set MyRange = Worksheets("SheetData").Range("E2:AB2")
countNonBlank = Application.WorksheetFunction.CountA(MyRange)

zLastCol = countNonBlank

For MyCol = 1 To zLastCol

If Cells(MyCol, 2).Value = "5" Then
Cells(MyCol, 2).ColumnWidth = "5"

Else
Cells(MyCol, 2).ColumnWidth = "0"

End If

Next MyCol

End Sub

Your help is greatly appreciated.
 
@Fluff,

Your code (and the OP's) will run for every recalculation of the sheet whether cell D6 triggered the recalculation or not. That means the columns are continuously being reset even when not needed.
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Yes I know.
The OP seemed happy with that code, so I simply removed the redundant portion.
 
Upvote 0

Forum statistics

Threads
1,215,565
Messages
6,125,583
Members
449,237
Latest member
Chase S

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