Code to adjust column width

P

primed

Hi,

Can anyone help with code to do the following:

Row E8:AI8 - each cell contains a value 0 to 100, generally a value of 1 to
20. The total of the row is maximum 100.
I would like to adjust the width of the column based on the values for each
column.
The minimum width would need to be 6 so text is still readable.

Thanks in advance

Primed
 
P

Per Jessen

Hi Primed

This should do it:

Sub ColumnAdjust()
Set Rng = Range("E8:AI8")
For Each cell In Rng
If cell.Value >= 6 Then
cell.ColumnWidth = cell.Value
Else
cell.ColumnWidth = 6
End If
Next
End Sub

Regards,
Per
 
P

primed

I am not a programmer but that doesnt seem to take into account the values in
row 8.
It will come in handy for something else though.

Cheers
Primed
 
P

primed

Cheers,

Worked great.
I modified slightly.

Sub ColumnAdjust()
Set Rng = Range("E8:AI8")
For Each cell In Rng
If cell.Value >= 1 Then
cell.ColumnWidth = cell.Value * 6
Else
cell.ColumnWidth = 0
End If
Next
End Sub
 
R

Rick Rothstein

I'm not sure why you say that... the code line I posted sizes each column in
the range to the contents of the widest value in the column (which, unless I
completely misunderstood what you were asking for, I believe is what your
initial posting was suggesting you wanted to do). If you didn't already do
so, give it a try and see how it works.
 
P

primed

Hi Rick,

Probably my poor communication skills are the issue. Columns E to AI need to
be resized based on values in row 8 only. Other rows contain data that needs
to be ignored.

Your formula seems to resize the columns based on the value in all the rows.

Regards
Primed
 
R

Rick Rothstein

Ah, I missed the Row 8 part. Give this a try then...

Range("E8:AI8").Columns.AutoFit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top