将名称ComboBox1更改为ComboBox6,并保持代码执行[保留]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将名称ComboBox1更改为ComboBox6,并保持代码执行[保留]相关的知识,希望对你有一定的参考价值。

我对在网上找到的这个依赖的ComboBox源代码有疑问。一切工作正常,除了我必须使用ComboBox1ComboBox2ComboBox3以外的其他组合框名称,因为我已经将它们用于其他目的。以我有限的编码技巧,我认为问题出在For循环中。这是代码的链接:

https://www.thesmallman.com/cascading-drop-down-lists

Private Sub Workbook_Open()
Dim ar As Variant
Dim j As Integer
Dim str As String

ar = Sheet1.Range("A10").CurrentRegion

    For j = 2 To UBound(ar)
        If InStr(str & ",", "," & ar(j, 1) & ",") = 0 Then str = str & "," & ar(j, 1)
    Next

    Sheet1.ComboBox1.List = Split(Mid(str, 2), ",")
    Sheet1.ComboBox2.Clear
    Sheet1.ComboBox3.Clear

End Sub



Private Sub ComboBox1_Change()
    ComboBox2.ListIndex=-1
    ComboBox3.ListIndex=-1
    If ComboBox1.ListIndex > -1 Then
        ComboBox2.List=Split(Cmbo(1), ",")
    End if
End Sub

Private Sub ComboBox2_Change()
    If ComboBox2.ListIndex > -1 Then
        ComboBox3.List=Split(Cmbo(2), ",")
    End If
End Sub

Function Cmbo(j)
    Dim n As Integer
    Dim i As Integer
    Dim ar As Variant
    Dim str As String

    ar=Range("A10").CurrentRegion

    For i = 1 To UBound(ar)
        For n = 1 To j
            If ar(i, n) <> Sheet1.OLEObjects("ComboBox" & n).Object.Value Then Exit For
        Next n
        If n = j + 1 And InStr(str & ",", "," & ar(i, n) & ",") = 0 Then
            str=str & "," & ar(i, n)
        End If
    Next

    Cmbo=Mid(str, 2)
End Function

[如果有人能向我展示如何将它们分配给ComboBox6ComboBox7ComboBox8,我将深表感谢。预先谢谢你。

答案

除了蒂姆的建议,是寻找并替换为您的真实姓名,例如找到Comboxbox1并替换为Combobox6,还需要查找"Combobox" & n或类似名称。

有多种方法可以引用工作表上的ActiveX对象。

  • 事件过程名称具有对象的名称。因此,Combobox1_Change需要成为Combobox6_Change。查找和替换应该为您完成。
  • 作为工作表的属性,例如Sheet1.Combobox1.Value需要成为Sheet1.Combobox6.Value。再次查找并替换应该修复它。
  • 您可以按其顺序位置进行引用。 Sheet1.Comboboxes(1)将返回第一个组合框。我在您的代码中看不到任何信息。
  • 您可以按名称将其称为字符串。 Sheet1.Comboboxes(“ Combobox1”)将返回名为Combobox1的对象。在您的代码中,您要将变量n连接到字符串Combobox来做到这一点。

在最后一种情况下,将n更改为n + 5。这将导致For循环中的字符串串联为Combobox6,Combobox7和Combobox8。

这是您的代码的外观

Private Sub Workbook_Open()
Dim ar As Variant
Dim j As Integer
Dim str As String

ar = Sheet1.Range("A10").CurrentRegion

    For j = 2 To UBound(ar)
        If InStr(str & ",", "," & ar(j, 1) & ",") = 0 Then str = str & "," & ar(j, 1)
    Next

    Sheet1.ComboBox6.List = Split(Mid(str, 2), ",")
    Sheet1.ComboBox7.Clear
    Sheet1.ComboBox8.Clear

End Sub


Private Sub ComboBox6_Change()
    Me.ComboBox7.ListIndex = -1
    Me.ComboBox8.ListIndex = -1
    If Me.ComboBox6.ListIndex > -1 Then
        Me.ComboBox7.List = Split(Cmbo(6), ",")
    End If
End Sub

Private Sub ComboBox7_Change()
    If Me.ComboBox7.ListIndex > -1 Then
        Me.ComboBox8.List = Split(Cmbo(7), ",")
    End If
End Sub

Function Cmbo(j)
    Dim n As Integer
    Dim i As Integer
    Dim ar As Variant
    Dim str As String

    ar = Range("A10").CurrentRegion

    For i = 1 To UBound(ar)
        For n = 1 To j
            If ar(i, n) <> Me.OLEObjects("ComboBox" & n + 5).Object.Value Then Exit For
        Next n
        If n = j + 1 And InStr(str & ",", "," & ar(i, n) & ",") = 0 Then
            str = str & "," & ar(i, n)
        End If
    Next

    Cmbo = Mid(str, 2)
End Function

以上是关于将名称ComboBox1更改为ComboBox6,并保持代码执行[保留]的主要内容,如果未能解决你的问题,请参考以下文章

Select2 是不是允许将“文本”键的名称更改为其他名称?

使用 Mongodb 聚合如何将数字更改为月份名称

WordPress:如何将类别页面标题更改为仅类别名称?

jQuery UI datepicker:如何将下拉列表中的月份名称从短名称更改为长名称?

将月份编号更改为月份名称

将 UITableView 的标题更改为 iOS 中的单元格名称