changing column widths by selecting cell in that column

keithmct

Active Member
Joined
Mar 9, 2007
Messages
254
Office Version
  1. 2021
Platform
  1. Windows
I have 10 columns which don't fit across my screen. How can I have the width small when not using that column, then as I click/type anywhere in the column it increases in width only to reduce in width when one of the other columns is clicked or typed in?
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello,

use below code in Selection Change event of Worksheet. It will autofit the column according to the cell you are working. Not sure if it fulfils the requirement.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim totalColumns As Integer, colno As Integer
    totalColumns = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Columns.ColumnWidth = 10
    Target.Columns.AutoFit
End Sub

1645079616631.png
 
Upvote 0
Possibly this would suffice?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.UsedRange.ColumnWidth = 10
    Target.EntireColumn.AutoFit
End Sub
 
Upvote 0
Possibly this would suffice?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.UsedRange.ColumnWidth = 10
    Target.EntireColumn.AutoFit
End Sub
not quite. I didn't account for some of my columns being different widths and must remain so. I'll have to think about it some more before asking more questions, thanks anyway.
 
Upvote 0
I didn't account for some of my columns being different widths and must remain so.
Would this be better, assuming 10 columns as mentioned originally?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim myWidths As Variant
  Dim i As Long
  
  myWidths = Split("5 12 5 5 4 3 7 5 7 7")  '<- These are the 'normal' column widths
  Application.ScreenUpdating = False
  For i = 0 To UBound(myWidths)
    Columns(i + 1).ColumnWidth = myWidths(i)
  Next i
  Target.EntireColumn.AutoFit
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Would this be better, assuming 10 columns as mentioned originally?

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim myWidths As Variant
  Dim i As Long
 
  myWidths = Split("5 12 5 5 4 3 7 5 7 7")  '<- These are the 'normal' column widths
  Application.ScreenUpdating = False
  For i = 0 To UBound(myWidths)
    Columns(i + 1).ColumnWidth = myWidths(i)
  Next i
  Target.EntireColumn.AutoFit
  Application.ScreenUpdating = True
End Sub
so close. I changed it a bit as follows

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim myWidths As Variant
  Dim i As Long
 
  myWidths = Split("10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4")  '<- These are the 'normal' column widths
  Application.ScreenUpdating = False
  For i = 0 To UBound(myWidths)
    Columns(i + 1).ColumnWidth = myWidths(i)
  Next i
  Target.ColumnWidth = 40
  Application.ScreenUpdating = True
End Sub

the columns that are 10 width are just for input of an appointment time and never need to change & I would prefer they didn't change width. The 0.4 ones are just dividers and also will never be clicked on. Otherwise it works perfectly. Having said that, this is a 265 sheet appointment diary so I need to get the code to work on all sheets. Do I put the code in This Workbook?
I would like to post xl2BB here but now if I select a section the columns all go to 40 wide. here is another sheet without the code if that helps. disregard the cell formulas
column change width experiment.xlsm
ABCDEFGHIJKL
2STEVERICKYLUKEELI
3Tuesday, 4 January 2022Tuesday, 4 January 2022Tuesday, 4 January 2022Tuesday, 4 January 2022
4timedetailstimedetailstimedetailstimedetails
5you need - you need - you need - you need -
6
78.30VGNMCGHM zdfhsdghdsh zdfdfbzdgbzdbz
8CHNCVHM
9VBNCVN
10
11
12
13
149.30VBNCVBN
15
16
17
1810.30CVBNCVNCVN
19
2011.00XBVCNCVBNCV
21
22
Tuesday 04 Jan 2022
Cell Formulas
RangeFormula
B3B3=A1
E3E3=+A1
H3H3=A1
L3L3=A1
 
Upvote 0
I am gathering that you really only want/need this to work when a single cell is selected?
I have assumed that for now anyway.
Remove any Worksheet_Change event codes dealing with this issues from sheet modules and place this in the ThisWorkbook module

VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  Dim myWidths As Variant
  Dim NumCols As Long, TargetCol As Long, i As Long
  
  If Target.CountLarge = 1 Then
    myWidths = Split("10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4")  '<- These are the 'normal' column widths
    NumCols = UBound(myWidths) + 1
    TargetCol = Target.Column
    Application.ScreenUpdating = False
    For i = 0 To NumCols - 1
      Columns(i + 1).ColumnWidth = myWidths(i)
    Next i
    If Target.Column <= NumCols Then
      If myWidths(TargetCol - 1) <> 0.4 And myWidths(TargetCol - 1) <> 10 Then Target.ColumnWidth = 40
    End If
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Solution
I am gathering that you really only want/need this to work when a single cell is selected?
I have assumed that for now anyway.
Remove any Worksheet_Change event codes dealing with this issues from sheet modules and place this in the ThisWorkbook module

VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  Dim myWidths As Variant
  Dim NumCols As Long, TargetCol As Long, i As Long
 
  If Target.CountLarge = 1 Then
    myWidths = Split("10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4 10 25 0.4")  '<- These are the 'normal' column widths
    NumCols = UBound(myWidths) + 1
    TargetCol = Target.Column
    Application.ScreenUpdating = False
    For i = 0 To NumCols - 1
      Columns(i + 1).ColumnWidth = myWidths(i)
    Next i
    If Target.Column <= NumCols Then
      If myWidths(TargetCol - 1) <> 0.4 And myWidths(TargetCol - 1) <> 10 Then Target.ColumnWidth = 40
    End If
    Application.ScreenUpdating = True
  End If
End Sub
you, Peter, are a legend. Thanks.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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