I'm using what is similar VBA I see in examples on the web of a ComboBox which looks at the validation in the cell and retrieves the list for the ComboBox. However, the code is designed for all the lists on the same worksheet. I have the lists on a different sheet. If I explicitly fill the ListFillRange it works of course for a single validation list, but the idea is the ComboBox appears in the active cell and if there is validation it reads the source. I'm hoping somebody can explain more how this works so I can mod the code to look at the lists on the other worksheet.
'.ListFillRange = "'" & Sheets("Lists").Name & ";!" & Sheets("Lists").Range(str).Address
.ListFillRange = ws.Range(str).Address
I'll encase all the code and even leave in my commented out last attempt... I'm not good but I'm trying. Thanks for any help.
'.ListFillRange = "'" & Sheets("Lists").Name & ";!" & Sheets("Lists").Range(str).Address
.ListFillRange = ws.Range(str).Address
I'll encase all the code and even leave in my commented out last attempt... I'm not good but I'm trying. Thanks for any help.
Code:
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'Hide combo box and move to next cell on Enter and Tab
Select Case KeyCode
Case 9
ActiveCell.Offset(0, 1).Activate
Case 13
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
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
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Cancel = True
End If
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
'.ListFillRange = "'" & Sheets("Lists").Name & ";!" & Sheets("Lists").Range(str).Address
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Last edited: