I’m currently working on a form within an Excel spreadsheet for a client.
The drop down requires information to be pulled out from two columns on the lookupDept workbook and merged. For instance I have:
deptCode deptName
BS Business School
CD Design and Technology
CG Chemical Engineering
CM Chemistry
CO Computer Science
and this needs to appear on the drop down as
BS - Business School
CD - Design and Technology
CG - Chemical Engineering
CM - Chemistry
CO - Computer Science
This is the code I’m currently using, unfortunately it does not seem to work and returns a Compile Error: Invalid Next control variable reference. Here is the code I’m using so far:
Private Sub UserForm_Initialize()
Dim c_deptCode As Range
Dim c_deptName As Range
Dim ws_dept As Worksheet
Set ws_dept = Worksheets("lookupDept")
For Each c_deptCode In ws_dept.Range("deptCode")
With Me.cbo_deptCode
.AddItem c_deptCode.Value
.List(.ListCount - 1, 1) = c_deptCode.Offset(0, 1).Value
End With
Next c_deptCode
For Each c_deptName In ws_dept.Range("deptName")
With Me.cbo_deptCode
.AddItem c_deptName.Value
End With
Next cLoc
End Sub
Thanks in advance!
In your second
Forloop, you are callingNext cLoc, but the loop is initialized asFor Each cDeptName. Try changing theNexttoNext cDeptNameto advance the correct variable.