Vba code for an autocomplete data validation with dependent lists

cwrivers

New Member
Joined
Jul 2, 2018
Messages
4
I have been using a code i found on Extendoffice that will turn a combo box into a drop down list using data validation and autocomplete and it has been working great, however my worksheet needs some additional organization. I would like to add a if statement into the data validation to only look at a certain range if a value in another cell is selected. The VBA code that i have been using will not support it. I cant send out the file that im using due to it containing sensitive information but i can supply the codes that im using.
This code works by itself in a data validation cell with in cell drop down,
=IF($W$12="XXXX",WOGCSPN,IF($W$12="YYYY",HUNTSPN,IF($W$12="AAAA",KRAKENSPN,IF($W$12="BBBB",LIBSPN,IF($W$12="CCCC",CONTSPN,$W$12)))))
The XYABC are all company names that would be input into another cell. Then when company XXXX is selected then the data validation only looks at that named range.
However when i add this VBA code to it, the drop down only shows one blank entry. (the same VBA code i have been using without using named ranges or an if statement)
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Update by Extendoffice: 2017/8/15
Dim xCombox As OLEObject
Dim xStr As String
Dim xWs As Worksheet
Set xWs = Application.ActiveSheet
On Error Resume Next
Set xCombox = xWs.OLEObjects("TempCombo")
With xCombox
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
If Target.Validation.Type = 3 Then
Target.Validation.InCellDropdown = False
Cancel = True
xStr = Target.Validation.Formula1
xStr = Right(xStr, Len(xStr) - 1)
If xStr = "" Then Exit Sub
With xCombox
.Visible = True
.Left = Target.Left
.Top = [URL="https://Target.Top"]Target.Top[/URL]
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
.LinkedCell = Target.Address
End With
xCombox.Activate
Me.TempCombo.DropDown
End If
End Sub
Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 9
Application.ActiveCell.Offset(0, 1).Activate
Case 13
Application.ActiveCell.Offset(1, 0).Activate
End Select
End Sub
Is there any way to make a drop down data validation cell with autocomplete, while using an if statement in the data validation source?
I have been beating my keyboard trying to figure this out, Thank you in advance for the help!
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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