ColumnWidth

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi,
want to change ColumnWidth with variable.
not working.
VBA Code:
Dim lastcol As Long
Set sht4 = Worksheets("Sheet4")
lastcol = sht4.Cells(4, Columns.Count).End(xlToLeft).Column

Columns("6: lastcol").ColumnWidth = 10
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You have not referenced the columns correctly. Try this change

Rich (BB code):
Columns("6: lastcol").ColumnWidth = 10
Columns(6).Resize(, lastcol - 5).ColumnWidth = 10

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
thanks Peter,
it works for column,
i tried for row, not working'
want to EntireRow.AutoFit from row 5 to lastrow
VBA Code:
Rows(5).Resize(, lastrow - 4).EntireRow.AutoFit
 
Upvote 0
i tried for row, not working'
For rows, you have to resize vertically, not horizontally, so remove the comma from the resize parentheses.
Also, since you are already referring to whole rows there is no need for entire row

Rich (BB code):
Rows(5).Resize(, lastrow - 4).EntireRow.AutoFit
Rows(5).Resize(lastrow - 4).AutoFit
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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