Forum Discussion
DJ_Excel
Aug 06, 2021Copper Contributor
Run macro if columns in a specific line are selected
Hi, I barelly remeber how to write VBA so i tend to find scripts on the internet. I found one that i wanted to modify but i cant seem to make the correct changes. Essentially i want to add a co...
- Aug 06, 2021
I see. Here is a new version:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Range("H1:AB1").ColumnWidth = 15 If Target.Count > 1 Then Exit Sub If Target.Row <> 65 Then Exit Sub If Target.Column >= 8 And Target.Column <= 28 Then Target.ColumnWidth = 50 End If End Sub
HansVogelaar
Aug 06, 2021MVP
It's a bit simpler:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Row <> 65 Then Exit Sub
If Target.Column >= 8 and Target.Column <= 28 Then
Target.ColumnWidth = 50
Else
Target.ColumnWidth = 15
End If
End Sub
- DJ_ExcelAug 06, 2021Copper ContributorThanks HansVogelaar , this is much more elegant.
It doesn't seem to go back to size 15 after i click out though...- HansVogelaarAug 06, 2021MVP
I see. Here is a new version:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Range("H1:AB1").ColumnWidth = 15 If Target.Count > 1 Then Exit Sub If Target.Row <> 65 Then Exit Sub If Target.Column >= 8 And Target.Column <= 28 Then Target.ColumnWidth = 50 End If End Sub
- DJ_ExcelAug 09, 2021Copper Contributor
Thank you so much HansVogelaar !
I do not understand how but it works!