2012-10-06 73 views
0

我目前正在爲客戶端在Excel電子表格中處理表格。Excel下拉菜單需要從兩列中提取信息

該下拉菜單需要信息從查找工作簿上的兩列中拉出併合並。比如我有:

deptCode deptName 
BS  Business School 
CD  Design and Technology 
CG  Chemical Engineering 
CM  Chemistry 
CO  Computer Science 

,這需要出現在下拉作爲

BS - Business School 
CD - Design and Technology 
CG - Chemical Engineering 
CM - Chemistry 
CO - Computer Science 

這是我目前使用,遺憾的是它似乎不工作的代碼,並返回一個編譯錯誤:無效下一個控制變量參考。這裏是我目前使用的代碼:

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 

在此先感謝!

回答

2

在您的第二個For循環中,您調用Next cLoc,但循環初始化爲For Each cDeptName。嘗試更改NextNext cDeptName以推進正確的變量。