Need to adjust the width of a Data Validation dropdown list

harrysolomon

New Member
Joined
Mar 6, 2002
Messages
25
I have a workbook which contains 6 worksheets and in each of those worksheets there are multiple columns which contain Data Validation Dropdown lists.

I want to be able to adjust the width of the dropdown lists when the users click on them so they can see the values to be selected and then when they select a value, I'd like the column to auto adjust it's width to the selected value.

I found some code online which does this great but I can only use the code for a single column, in
the following example it only works on the dropdown list in Column 7.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 7 Then
Target.Columns.ColumnWidth = 20
Else
Columns(7).AutoFit
End If
End Sub


How can I change this so it can be used on other columns? For example, I want it to work on the lists where Target.Column = 3,
or Target.Column = 7 or Target.Column = 8 or Target.Column = 9.

I'm also going to want to use this across all the worksheets in the workbook but I'm guessing that would just be a matter of assigning this code to each worksheet?

Thanks.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello,

try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
        Select Case Target.Column
            Case 3, 7, 8, 9
                Target.Columns.ColumnWidth = 20
            Case Else
                Columns(Target.Column).AutoFit
        End Select
End Sub
 
Upvote 0
Hello,

try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
        Select Case Target.Column
            Case 3, 7, 8, 9
                Target.Columns.ColumnWidth = 20
            Case Else
                Columns(Target.Column).AutoFit
        End Select
End Sub



Thanks but this isn't working correctly on my worksheet.
When I click on one of the specified columns, the width is not auto-adjusting to 20 for some reason.
 
Upvote 0
Hello,

Sorry, forgot to mention that the code needs to go into the sheet code window, not a standard module.

You will need to copy it into every sheet code window.
 
Upvote 0
The original code was

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

So drafter's solution should be adjusted accordngly.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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