VBA Macro for Userform

Zanaba

New Member
Joined
Feb 22, 2018
Messages
2
I have an excel workbook that I have made a userform for. My plan for this form is to have the user pick a location from the first combo box and pick another location from the second combo box. After picking these two locations the user will click the run button to run the macro.

Example of Sheet1 data going down will grow, but width of table will not go past column F.

ABCDEF
Miami, FLOakland, CANew York, NYChicago, IL
TMSNashville, TN$100$200$300$100
ATSMemphis, TN$200$300$100$150
NTFresno, CA$70$120$152$170
NTMemphis, TN$50$70$100$150
ATSNashville, TN$100$150$80$90
TMSFresno, CA$200$70$300$250

<tbody>
</tbody>


So the first combo box, the user will chose either Miami, FL Oakland, CA New York, NY or Chicago IL

Second combo box, the user will choose from the other locations(Nashville, Memphis, Fresno)

So user chooses Miami, FL and Fresno, CA and clicks start.

The macro will then search Sheet1 and find all the results for Miami, FL and Fresno, CA and will make a new sheet and return the results for all matching the two locations.

Result Example:
COLUMN ACOLUMN BCOLUMN C
Miami, FL
NTFresno, CA$70
TMSFresno, CA$200

<tbody>
</tbody>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
What code do you have so far?
 
Upvote 0
I have the code that can move things around. Just struggling getting it to search based off the options picked in the combolists and returning the results in a new sheet. I am not sure how to get the macro to, not sure of the right word, but read what is in the combo lists and search based off of that.
 
Upvote 0
Could you post your existing code?
 
Upvote 0
Try
Code:
[COLOR=#0000ff]Option Explicit
Private Dic As Object[/COLOR]

Private Sub StartBtn_Click()
   Dim Orow As Variant
   Dim Drow As Long
   
   Drow = 2
   Sheets.Add
   Range("C1").Value = ComboBox1.Value
   For Each Orow In Split(Dic(ComboBox2.Value), ",")
      With Sheets("Sheet1")
         .Range("A" & Orow).Resize(, 2).Copy ActiveSheet.Range("A" & Drow)
         .Cells(Orow, ComboBox1.ListIndex + 3).Copy ActiveSheet.Range("C" & Drow)
         Drow = Drow + 1
      End With
   Next Orow
End Sub

Private Sub UserForm_Initialize()

   Dim Cl As Range

   Set Dic = CreateObject("scripting.dictionary")

   With Sheets("Sheet1")
      If .AutoFilterMode Then .AutoFilterMode = False
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not Dic.exists(Cl.Value) Then
            Dic.Add Cl.Value, Cl.Row
         Else
            Dic(Cl.Value) = Dic(Cl.Value) & "," & Cl.Row
         End If
      Next Cl
   End With
   ComboBox2.list = Application.Transpose(Dic.keys)
   ComboBox1.list = Application.Transpose(Range("C1:F1"))
End Sub
The 2 lines in blue MUST go at the very top of the module (before any code).
Change the 2 combobox names & the Start button name if needed.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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