热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Iwanttofillthesecondcomboboxbasedonthefirstcombobox.Thefirstcomboboxhavetheseval

I want to fill the second combo box based on the first combo box.
The first combo box have these values: John, Marry, Lona, Fred

我想基于第一个组合框填充第二个组合框。第一个组合框有这些值:John,Marry,Lona,Fred

    A         B
1  John      384
2  John      475
3  John      450
4  Marry     616
5  Marry     526
6  Lona      569
7  Lona      234
8  Lona      937
9  Lona      477
10 Fred      286

For example when I choose John in combobox1, there should be these values in combobox2: 384,475,450
My code doesn't work:

例如,当我在combobox1中选择John时,在combobox2中应该有这些值:384,475,450我的代码不起作用:

Private Sub ComboBox1_change()

Set rngItems = Sheet1.Range("B1:B10")
Set oDictiOnary= CreateObject("Scripting.Dictionary")

With Sheet2.ComboBox2
    For Each cel In rngItems
        If ComboBox1.Value = cel.Value Then

            oDictionary.Add cel.Value, 0
            .AddItem cel.Value
        End If
    Next cel
End With
End Sub

2 个解决方案

#1


2  

Your code as it stands isn't working because you are searching Column B, the column with the numeric values, for a match to the names. To fix, use the OFFSET function to check the cell to the left. For example:

您的代码不起作用,因为您正在搜索列B(具有数值的列),以匹配名称。要修复,请使用OFFSET函数检查左侧的单元格。例如:

Private Sub ComboBox1_change()

Set rngItems = Sheet1.Range("B1:B10")
Set oDictiOnary= CreateObject("Scripting.Dictionary")

With Sheet2.ComboBox2
    For Each cel In rngItems
        If ComboBox1.Value = cel.Offset(,-1).Value Then

            oDictionary.Add cel.Value, 0
            .AddItem cel.Value
        End If
    Next cel
End With
End Sub

#2


1  

The problem you are having is that you are setting rngItems to column B. What you want to do is set it to column A, and then get the value from column B. Try this:

您遇到的问题是您将rngItems设置为B列。您要做的是将其设置为A列,然后从B列中获取值。试试这个:

Set rngItems = Sheet1.Range("A1:A10") 
Set oDictiOnary= CreateObject("Scripting.Dictionary")

With Sheet2.ComboBox2
    For Each cel In rngItems
        If ComboBox1.Value = cel.Value Then
            oDictionary.Add Cells(cel.row, cel.column + 1).Value, 0
            .AddItem Cells(cel.row, cel.column + 1).Value            
        End If
    Next cel
End With

End Sub


推荐阅读
author-avatar
烟为你吸_811
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有