根据第一个组合框填充第二个组合框

时间:2017-11-07 12:29:54

标签: vba excel-vba excel

我在Excel用户表格中有2个组合框,我需要第一个组合框来影响第二个组合列出的内容。我从excel中的工作表上获取数据的表看起来像附加图像:

enter image description here

数据继续与公司名称和个人联系。我希望combobox2做的是在combobox1中选择DT受限时,combobox2只显示john和steve以供选择。

任何人都可以帮忙,我是新来的吗?

2 个答案:

答案 0 :(得分:0)

Private Sub UserForm_Initialize()
With ComboBox1 'Your first combobox
.Clear


.AddItem "Users" 
.AddItem "Salary"   ' Add main combobox Category

End With
End Sub



Private Sub ComboBox1_Change()

Dim index As Integer
 index = ComboBox1.ListIndex

 ComboBox2.Clear ' clear your dependent combobox

Select Case index
     Case Is = 0   ' Add Subcategory Display items
         With ComboBox2
         .AddItem "John"
         .AddItem "Angie"
         .AddItem "Sam"
     End With
 Case Is = 1
     With ComboBox2
         .AddItem "20000"
         .AddItem "45000"
         .AddItem "80000"
     End With

End Select

End Sub

答案 1 :(得分:0)

此代码将解决您的问题。

from xlrd import open_workbook

wb = open_workbook('D:\abc_Template.xlsx', 'r')

wb_sheet = wb.sheet_by_index(0)

values = []

for row_idx in range(7, wb_sheet.nrows):
    col_value = []
    rowval = str((wb_sheet.cell(row_idx, 1)))

    for col_idx in range(1, 5):
        if(col_idx != 2 and col_idx != 1):
            cellVal = wb_sheet.cell(row_idx, col_idx)
            cellObj = {rowval: {"value" : cellVal}}
            col_value.append(cellObj)

    values.append(col_value)

print values