use VBA code to create Excel drop down list

raschwab

New Member
Joined
Mar 26, 2011
Messages
15
I am endeavouring to create and store a dropdown list in a worksheet cell as opposed to using the traditional excel 2007 methodology. The list currently contains seven items. I have played with a few lines of code and cannot seem to get it to work. Also, I would like to be able to wrap the list of list elements in more than one line of code to reduce code width. One of the pieces of code that I have attempted is;
Code:
  With Range("A5").Validation
        .Delete
        .Add Type:=xlValidateList, Operator:=xlBetween, Formula:="ListItem1," & _
                "ListItem2," & _
                "ListItem3," & _
                "ListItem4," & _
                "ListItem5," & _
                "ListItem6," & _
                "ListItem7"
    End With

Any help would be very much appreciated.

Rick
 
This will place Data Validation in cell A5 with a list of items ListItem1 through ListItem7 as you asked.


Code:
Sub DVraschwab()
Dim myList$, i%
myList = ""
For i = 1 To 7
myList = myList & "ListItem" & i & ","
Next i
myList = Mid(myList, 1, Len(myList) - 1)
With Range("A5").Validation
.Delete
.Add _
Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Formula1:=myList
End With
End Sub
This post really came in handy with creating my long awaited vba-based drop-down list solution...

VBA Code:
Sub Planner_List_Period()

  'Declarations
  Dim Grade As Integer, Qtr As Integer
  Dim mpList As String: mpList = "" 'Marking Period List

  'Get Active Grade
  Grade = WorksheetFunction.XLookup(Range("t_Period_Active[Name]"), _
    Range("t_Students[Name]"), Range("t_Students[Grade]"))

  'Build Drop-Down List Criteria
  For Qtr = 1 To 4
    mpList = mpList & Grade & "." & Qtr & ","
  Next Qtr

  mpList = Mid(mpList, 1, Len(mpList) - 1)
  
  'Add Drop-Down List
  With Range("t_Period_Active[Period]").Validation
    .Delete
    .Add _
    Type:=xlValidateList, _
    AlertStyle:=xlValidAlertStop, _
    Formula1:=mpList
  End With
  
  'Insert Active Period
  Range("t_Period_Active[Period]").Value = WorksheetFunction.XLookup(Range("t_Period_Active[Name]"), _
    Range("t_Students[Name]"), Range("t_Students[Period]"))
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,798
Messages
6,193,065
Members
453,773
Latest member
bclever07

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