VBA Autofit Columns

ionelz

Board Regular
Joined
Jan 14, 2018
Messages
248
Office Version
  1. 365
Platform
  1. Windows
I have an Button for this : to autofit columns, from 8 to....how many they are (found on net)

Dim x As Integer
For x = 8 To ActiveSheet.UsedRange.Columns.Count
Columns(x).EntireColumn.AutoFit
Next x

All Ok, it autofit columns but it change the SIZE too of my Active X Control buttons placed in these columns even they are set don't move or size with cells
How to fix this ?
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Columns or rows?

I use the below for my table, maybe you can adjust to suit yours?

"FAC_RISKS" being the defined range.

VBA Code:
Sub Autofit()
  Dim rng As Range
  Application.ScreenUpdating = False
  ActiveSheet.Rows("1").RowHeight = 120
  ActiveSheet.Rows("2").RowHeight = 50
  ActiveSheet.Range("FAC_RISKS").EntireColumn.Autofit
  ActiveSheet.Range("FAC_RISKS").EntireRow.Autofit
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Upvote 0
This doesn't solve your issue, but just a little note that will help you with your programming and performance.
When auto fitting many consecutive columns, you do NOT need to loop through each one. You can apply autofit to all those columns at once with a single line of code, i.e.
VBA Code:
    Range(Cells(1, 8), Cells(1, ActiveSheet.UsedRange.Columns.Count)).EntireColumn.AutoFit
 
Upvote 0
Did this but still buttons change in size

Size.xlsb
Did you modify the property of each shape to "don't move or size with cells" ?
Because that's the solution to your problem.
See attachment, you can change columnwidth or rowheight, the shapes all remain at their position and size.
 
Upvote 0
Size.xlsb
Did you modify the property of each shape to "don't move or size with cells" ?
Because that's the solution to your problem.
See attachment, you can change columnwidth or rowheight, the shapes all remain at their position and size.
I can not download your file, and for some reason, "don't move or size with cells" do not work for me, buttons keep size ..ing
 
Upvote 0
What is the point of uploading another file that is totally irrelevant to the OP's problem?
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,794
Members
449,048
Latest member
greyangel23

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