Dependent dropdown list in excel using VBA Code

deba2020

New Member
Joined
Jan 8, 2020
Messages
26
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
I have a workbook with 2 sheets in it
Sheet1 Name = Vendor
Sheet2 Name = Payment

In Sheet(Vendor) I have details of the vendor base of my organisation
Vendor CodeSiteVendor Name
1AUSTRALIASupplier1
2FLORIDASupplier2
3MALAYSIA.Supplier3
4P. R. CHINASupplier4
5GERMANYSupplier5
6JEDDAHSupplier6
7DUBAISupplier7
7SWITZERLANDSupplier7
7UNITED STATESSupplier7
7BJN-SWITZERLANDSupplier7


In Sheet(Payment) if I enter Vendor Code in any cell in Column D the VBA code should look Sheet(Vendor) for the vendor code and list the sites of the vendor in drop down list in Column H
DateDateRTGS/NEFT REFV CodePartyAmountTrading PartnerSite
104-Feb-2304424MSEDCLMISCELLANEOUS VENDOR


Should you have any query please ask.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Paste this code in a module
VBA Code:
Sub Lookupstuff(ByRef Finder As Range, SearchString As String)
        Dim wk1 As Worksheet
        Set wk1 = Sheets("Vendor")
        Dim V
        Dim K As Long
        
        On Error Resume Next
        With wk1
                V = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row)
                For K = 1 To UBound(V, 1)
                       If V(K, 1) = Finder.Value Then
                            SearchString = SearchString & "," & V(K, 2)
                        End If
                Next K
        End With
         SearchString = Right(SearchString, Len(SearchString) - 1)
        
        
        
End Sub

Paste this code in the "Payment" worksheet's worksheet change event

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        On Error Resume Next
        Dim Temp As String
        If Target.Cells.Count > 1 Then Exit Sub

        If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
                
                Target.Offset(0, 4).Validation.Delete           'it will clear all of the previous validations in the column H
                Target.Offset(0, 4).ClearContents
                Lookupstuff Target, Temp
                ActiveSheet.Range(Target.Offset(0, 4).Address(0, 0)).Validation.Add xlValidateList, , , Temp
                
End Sub
 

Attachments

  • 1675516101746.png
    1675516101746.png
    34.4 KB · Views: 6
Upvote 0
Solution
Paste this code in a module
VBA Code:
Sub Lookupstuff(ByRef Finder As Range, SearchString As String)
        Dim wk1 As Worksheet
        Set wk1 = Sheets("Vendor")
        Dim V
        Dim K As Long
       
        On Error Resume Next
        With wk1
                V = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row)
                For K = 1 To UBound(V, 1)
                       If V(K, 1) = Finder.Value Then
                            SearchString = SearchString & "," & V(K, 2)
                        End If
                Next K
        End With
         SearchString = Right(SearchString, Len(SearchString) - 1)
       
       
       
End Sub

Paste this code in the "Payment" worksheet's worksheet change event

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        On Error Resume Next
        Dim Temp As String
        If Target.Cells.Count > 1 Then Exit Sub

        If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
               
                Target.Offset(0, 4).Validation.Delete           'it will clear all of the previous validations in the column H
                Target.Offset(0, 4).ClearContents
                Lookupstuff Target, Temp
                ActiveSheet.Range(Target.Offset(0, 4).Address(0, 0)).Validation.Add xlValidateList, , , Temp
               
End Sub
Thank you very much, it worked like gem.
 
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,538
Members
449,456
Latest member
SammMcCandless

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