nrodrigues23
New Member
- Joined
- Apr 9, 2019
- Messages
- 14
I am trying to create a button through VBA Code that allows me to autofit every column in a worksheet based only on the range of cells that has numbers in it (Ie. prevents autofitting based on headers). I found a video online that gives me the following code but it needs to be modified in order to give me what I want.
Private Sub Worksheet_Change(ByVal Target As Range)
Cells.EntireColumn.AutoFit
For i = 1 To ActiveSheet.UsedRange.Columns.Count
Columns(i).ColumnWidth = Columns(i).ColumnWidth + 1
Next i
End Sub
I am new to writing code but this is what I am looking to do.
1. Create a button at the top of the screen to press when I want to adjust the width of columns. (I know how to assign a macro to a button)
2. I want to autofit the column and subtract from the autofitted width (my boss likes things tight on the page)
3. I want to autofit based only on the numbers in the columns and not the header (I have some merged headers)
Note: the code above seems to be adding "1" width to the column every time something changes on the page. I would like it to simply revert to autofit - 1 when I press the button.
Any help would be help me save a ton of time...
Private Sub Worksheet_Change(ByVal Target As Range)
Cells.EntireColumn.AutoFit
For i = 1 To ActiveSheet.UsedRange.Columns.Count
Columns(i).ColumnWidth = Columns(i).ColumnWidth + 1
Next i
End Sub
I am new to writing code but this is what I am looking to do.
1. Create a button at the top of the screen to press when I want to adjust the width of columns. (I know how to assign a macro to a button)
2. I want to autofit the column and subtract from the autofitted width (my boss likes things tight on the page)
3. I want to autofit based only on the numbers in the columns and not the header (I have some merged headers)
Note: the code above seems to be adding "1" width to the column every time something changes on the page. I would like it to simply revert to autofit - 1 when I press the button.
Any help would be help me save a ton of time...