Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("QuoteCombo")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains
'a data validation list
Cancel = True
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.QuoteCombo.DropDown
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub QuoteCombo_LostFocus()
With Me.QuoteCombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
' ZVI:2019-02-07 https://www.mrexcel.com/forum/excel-questions/1086525-expand-column-width-automatically-based-drop-down-selection.html
'--> Settings, change to suit
Const FitColumns = "C:D" ' Columns to fit
Const FirstDataRow = 24 ' Fit data are in that row and the below rows
'<--End of the settings
Dim a() As Variant, Col As Range, OldWidth
Application.EnableEvents = False
Application.ScreenUpdating = False
With Intersect(Target.EntireColumn, Me.Range(FitColumns))
For Each Col In .Columns
With Col.Resize(FirstDataRow - Col.Cells(1).Row)
a() = .Value
.Value = Empty
OldWidth = .ColumnWidth
.EntireColumn.AutoFit
If .ColumnWidth < OldWidth Then
.ColumnWidth = OldWidth
End If
.Value = a()
End With
Next
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub quotecombo_Change()
' Code for ComboBox1 ActiveX
Worksheet_Change Me.QuoteCombo.TopLeftCell.EntireColumn
End Sub