Macro to select next name in dropdown list

fordmudslinger

Board Regular
Joined
Apr 4, 2015
Messages
64
Thanks for your help in advance! I have a dropdown list with my customers name in it for invoicing. I am looking to assign a macro to a button that, once clicked, will change to the next customer name in the list. Is this possible?
 
Code:
[color=darkblue]Sub[/color] Button_Click()
    [color=darkblue]Dim[/color] v [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]With[/color] Sheets("Invoice").Range("C12")
        [color=darkblue]If[/color] .Value = "" [color=darkblue]Then[/color]
            .Value = Sheets("Customers").Range("A2").Value
        [color=darkblue]Else[/color]
            v = Application.Match(.Value, Sheets("Customers").Range("A2:A25"), 0)
            [color=darkblue]If[/color] IsNumeric(v) [color=darkblue]Then[/color]
                .Value = Sheets("Customers").Range("A2:A25").Cells(v + 1, 1).Value
            [color=darkblue]Else[/color]
                .Value = ""
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
You know how I like to condense things, right? :wink: This produces the same output as your code...
Code:
Sub Button_Click()
  Dim v As Variant, CustomerList As Variant
  With Sheets("Invoice").Range("C12")
    CustomerList = Join(Application.Transpose(Sheets("Customers").Range("A2:A25")), "|")
    CustomerList = "|" & CustomerList & "||" & CustomerList
    .Value = Split(Split(CustomerList, "|" & .Value & "|")(1), "|")(0)
  End With
End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You know how I like to condense things, right? :wink: This produces the same output as your code...
Code:
Sub Button_Click()
  Dim v As Variant, CustomerList As Variant
  With Sheets("Invoice").Range("C12")
    CustomerList = Join(Application.Transpose(Sheets("Customers").Range("A2:A25")), "|")
    CustomerList = "|" & CustomerList & "||" & CustomerList
    .Value = Split(Split(CustomerList, "|" & .Value & "|")(1), "|")(0)
  End With
End Sub
Even more compact...
Code:
Sub Button_Click()
  Dim CustomerList As Variant
  CustomerList = "||" & Join(Application.Transpose(Sheets("Customers").[A2:A25]), "|") & "||"
  With Sheets("Invoice").[C12]
    .Value = Split(Split(CustomerList, "|" & .Value & "|")(1), "|")(0)
  End With
End Sub
 
Last edited:
Upvote 0
Even more compact...
Code:
Sub Button_Click()
  Dim CustomerList As Variant
  CustomerList = "||" & Join(Application.Transpose(Sheets("Customers").[A2:A25]), "|") & "||"
  With Sheets("Invoice").[C12]
    .Value = Split(Split(CustomerList, "|" & .Value & "|")(1), "|")(0)
  End With
End Sub
This is probably as compact as I can make it... it's a one-liner (just remove the line continuation character and rejoin the two lines to make it look like one)...
Code:
Sub Button_Click()
  Sheets("Invoice").[C12] = Split(Split("||" & Join(Application.Transpose(Sheets( _
     "Customers").[A2:A25]), "|") & "||", "|" & Sheets("Invoice").[C12] & "|")(1), "|")(0)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,073
Members
449,205
Latest member
Healthydogs

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