Resize columns, excluding hidden rows

bfoxworthy

New Member
Joined
Jun 28, 2011
Messages
7
Is there any way to re-size all the columns on a worksheet to the widest visible cell in the column? Right now Excel re-sizes columns including hidden values.

Should be a simple solution?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Something like this maybe...

Code:
Dim A As Range
For Each A In Columns.SpecialCells(xlCellTypeVisible).Areas
  A.EntireColumn.AutoFit
Next
 
Upvote 0
Sorry, how exactly do i integrate that?
No, I'm sorry... for some reason, I thought you had an existing macro and the code I posted was meant to be integrated into it at whatever point you wanted to resize the column widths. Let's convert what I posted to a stand-alone macro so that you can call it whenever you want. First, from any worksheet, press ALT+F11. This will open the VB editor. Once there, click Insert/Module on its menu bar. This will open up a code window; copy/paste the following code into it...

Code:
Sub AutoFitVisibleColumnsOnly()
  Dim A As Range
  For Each A In Columns.SpecialCells(xlCellTypeVisible).Areas
    A.EntireColumn.AutoFit
  Next
End Sub
Now, to use this macro, go to the worksheet with the (visible) columns you want to autofit and press ALT+F8. This will open a dialog box... select AutoFitVisibleColumnsOnly from the list and then click the Run button.
 
Upvote 0
Just to follow up on my last message, you can do what you asked without using code. Press CTRL+G, click the "Special" button, select the "Visible cells only" option button and then click the "OK" button. Now, do your Columns AutoFit (the method depends on your version of Excel, but since you asked, I assume you know how to do this)... only the visibles will be resized.
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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