Fitting columns to text in autofiltered rows

El_Tel

New Member
Joined
Aug 18, 2009
Messages
1
I've written some VBA to create a new sheet based on a 'master' sheet then autofilter the new sheet according to certain criteria. This obviously hides some of the unwanted rows leaving the text in other rows visible.

I now want to change the width of some columns so that they will fit the text in the VISIBLE rows only. I thought this would be easy but I can't find a solution. I've tried Autofit but this resets the column width to fit the text in both visible and hidden rows. This leaves me with a lot of white space.

Can anyone help me on this please? I've googled the web without much joy.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I've written some VBA to create a new sheet based on a 'master' sheet then autofilter the new sheet according to certain criteria. This obviously hides some of the unwanted rows leaving the text in other rows visible.

I now want to change the width of some columns so that they will fit the text in the VISIBLE rows only. I thought this would be easy but I can't find a solution. I've tried Autofit but this resets the column width to fit the text in both visible and hidden rows. This leaves me with a lot of white space.

Can anyone help me on this please? I've googled the web without much joy.
Welcome to the MrExcel board!

I'm not sure of a direct or perfect way to do this, but see if this might be good enough. It may fall down depending on the font/character widths involved, but you may be able to compensate enough by adding a little 'extra' where indicated.

I have done this with just one column of interest (col C) but you should be able to adapt for more if required.

1. Set up two 'helper columns as I have done in columns H:I. Copy the col H formula down further than you are likely to want (though I guess this code - or better still, your existing code - could be adding/deleting these formulas as required).

Excel Workbook
ABCDEFGHI
1H1H2H3H4H52AAAAAAAAAA
2a1
3ab2
4AAAAAAAAAA10
5abcd4
6lkjuy5
7dsgtrih7
8lkjhgfr7
9lioujhyt8
10hgfrtdert9
110
120
AutoFit with AutoFilter




2. Hide columns H:I

3. Put this Worksheet_Calculate event code in the sheet module.

<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Calculate()<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Columns("I")<br>        .AutoFit<br>        Columns("C").ColumnWidth = .ColumnWidth <SPAN style="color:#007F00">'<-- Add extra here?</SPAN><br>        .Hidden = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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