Autopopulate ComboBox Value based on the value of TextBox

deba2020

New Member
Joined
Jan 8, 2020
Messages
26
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
I have a macro enabled excel workbook named Consolidation.xlsm.
1st sheet contains details of Material Received, Storage Location, GRN and Quantity, Name of the sheet : Sheet1
Data in the sheet1 looks like this
1630602011873.png


In sheet 2 data appears like this
1630603160088.png

Requirement:
Upon clicking on corresponding cell (Storage Location) a userform will pop up like this. which will automatically grab the RM Code in Textbox
1630603423327.png

Now in Combobox ("Location") we need list of those locations where D170 were stored as per the details available in Sheet1
and in GRN field we need list of those GRN of D170 based on locations selected in Combobox.

Please help.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
What code do you currently have for the userform?
 
Upvote 0
What code do you currently have for the userform?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Row < 5 Then Exit Sub
If Not Intersect(Range("d:d"), Target) Is Nothing And Target.Count = 1 Then
UserForm2.Left = Target.Left + 25
UserForm2.Top = Target.Top + 30 - Cells(ActiveWindow.ScrollRow, 1).Top
UserForm2.TextBox1.value = Target.Offset(0, -2)
UserForm2.Show
End If
End Sub
 
Upvote 0
Do you currently have any code in the userform itself?
 
Upvote 0
I prefer a ComboBox over a Textbox in your case, but you could study this.

VBA Code:
Private Sub ComboBox1_DropButtonClick()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
      For j = 1 To UBound(ar)
          If UCase(ar(j, 1)) = UCase(TextBox1.Value) Then c00 = c00 & "|" & ar(j, 2)
      Next
      ComboBox1.List = Application.Transpose(Split(Mid(c00, 2), "|"))
  End If
End Sub
 
Upvote 0
Or without loop

VBA Code:
Private Sub ComboBox1_DropButtonClick()
  Sheets(1).Range("A2", Sheets(1).Cells(Rows.Count, 1).End(xlUp)).Name = "a"
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  ComboBox1.List = Application.Index(ar, Filter(Evaluate("transpose(Iferror(Find(""" & UCase(TextBox1.Text) & """, a) * Row(a), ""~""))"), "~", False), 2)
End Sub
 
Upvote 0
I prefer a ComboBox over a Textbox in your case, but you could study this.

VBA Code:
Private Sub ComboBox1_DropButtonClick()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
      For j = 1 To UBound(ar)
          If UCase(ar(j, 1)) = UCase(TextBox1.Value) Then c00 = c00 & "|" & ar(j, 2)
      Next
      ComboBox1.List = Application.Transpose(Split(Mid(c00, 2), "|"))
  End If
End Sub
Thanks JEC, your code worked but is there anyway to get unique list of locations in combobox also request you to help me with the combobox for GRN.
 
Upvote 0
For unique (For GRN you can use the same method):

VBA Code:
Private Sub ComboBox1_DropButtonClick()
    ar = Sheets(1).Cells(1, 1).CurrentRegion
     If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
        For j = 1 To UBound(ar)
            If UCase(ar(j, 1)) = UCase(TextBox1.Value) And InStr(c00, ar(j, 2)) = 0 Then c00 = c00 & "|" & ar(j, 2)
        Next
        ComboBox1.List = Application.Transpose(Split(Mid(c00, 2), "|"))
     End If
End Sub

or

VBA Code:
Private Sub ComboBox1_DropButtonClick()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  With CreateObject("scripting.dictionary")
     If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
        For j = 1 To UBound(ar)
            If UCase(ar(j, 1)) = UCase(TextBox1.Value) Then c00 = .Item(ar(j, 2))
        Next
        ComboBox1.List = Application.Transpose(.keys)
     End If
  End With
End Sub
 
Upvote 0
Solution
For unique (For GRN you can use the same method):

VBA Code:
Private Sub ComboBox1_DropButtonClick()
    ar = Sheets(1).Cells(1, 1).CurrentRegion
     If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
        For j = 1 To UBound(ar)
            If UCase(ar(j, 1)) = UCase(TextBox1.Value) And InStr(c00, ar(j, 2)) = 0 Then c00 = c00 & "|" & ar(j, 2)
        Next
        ComboBox1.List = Application.Transpose(Split(Mid(c00, 2), "|"))
     End If
End Sub

or

VBA Code:
Private Sub ComboBox1_DropButtonClick()
  ar = Sheets(1).Cells(1, 1).CurrentRegion
  With CreateObject("scripting.dictionary")
     If IsNumeric(Application.Match(TextBox1.Value, Application.Index(ar, 0, 1), 0)) Then
        For j = 1 To UBound(ar)
            If UCase(ar(j, 1)) = UCase(TextBox1.Value) Then c00 = .Item(ar(j, 2))
        Next
        ComboBox1.List = Application.Transpose(.keys)
     End If
  End With
End Sub
Thanks Friend, it was indeed a great help. It worked like wonder.
 
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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