AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79593912
Accepted
Shiela
Shiela
Asked: 2025-04-26 20:28:13 +0800 CST2025-04-26 20:28:13 +0800 CST 2025-04-26 20:28:13 +0800 CST

如何在组合框更改期间删除一个值

  • 772

我这里有一个列表框,可以与下面的代码一起正常工作。

列表框1

列表框

更新了整个代码:

Option Explicit
Dim bInit As Boolean ' ** module-scoped variable

Private Sub UserForm_Initialize()
    bInit = True  ' ** set UserForm_Initialize mode **from Taller
    clearAll
    Me.cmbName.Value = "Nory"
    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim i As Long
    Dim arr: arr = ws.Range("B1").CurrentRegion.Value
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")

    For i = 2 To UBound(arr)
        dict(arr(i, 2)) = arr(i, 1)
    Next
   ' ***
     If dict.exists(Me.cmbName.Value) Then
        Me.cmbTeam.Value = dict(Me.cmbName.Value)
        If Not cmbTeam.Value = "" And Not cmbDate.Value = "" And Not cmbName.Value = "" Then
            Team
            forListBoxUpdate 'calling forListBoxUpdate
            forDateCombobox 'filling in date dropdowns
            forNamesCombobox 'filling in names combobox as long as 3 comboboxes are not blank
        Else
            Team 'calling Team dropdowns in case team is blank or default cmbName value has no team
            cmbDate.Value = ""
            cmbName.Value = ""
        End If
    Else
            Team 'calling Team dropdowns in case team is blank or default cmbName value has no team
            cmbDate.Value = ""
            cmbName.Value = ""
    End If
    bInit = False  ' ** reset **from Taller
End Sub

Private Sub cmbDate_Change()
    If Not cmbTeam = "" And Not cmbName = "" Then
        forListBoxUpdate 'calling to show info if team and name not blank
        forNamesCombobox 'filling in cmbname dropdowns
    Else
        cmbDate.Clear 'if cmbteam and cmbname blank, cmdate should also be blank
    End If
End Sub

Private Sub cmbName_Change()
    forListBoxUpdate
End Sub

Private Sub cmbTeam_Change()
    cmbDate.Value = ""
    'cmbName.Value = "" 'issue, during initialize, default cmbname is removed.
    clearAll 'used this instead
    forDateCombobox 'fills in date dropdowns
End Sub



Sub forListBoxUpdate() 'to show info from sheets and to be called after the 3 comboboxes are filled
    Dim ws As Worksheet, colList As Collection
    Dim arrData, arrList, i As Long, j As Long
    
    Set colList = New Collection
    Set ws = Worksheets("Sheet2")
    arrData = ws.Range("A1:C" & ws.Cells(ws.Rows.count, "A").End(xlUp).Row)
    
        For i = 2 To UBound(arrData)
            If Format(arrData(i, 1), "mmmm yyyy") = Me.cmbDate.Value And arrData(i, 3) = Me.cmbName.Value Then
                colList.Add i, CStr(i)
            End If
    Next

    ReDim arrList(1 To colList.count + 1, 1 To UBound(arrData))
    For j = 1 To 3
        arrList(1, j) = arrData(1, j) ' header
        For i = 1 To colList.count
                arrList(i + 1, j) = arrData(colList(i), j)
        Next
    Next

    With Me.ListBox1
        .Clear
        .ColumnCount = UBound(arrData, 2)
        .list = arrList
    End With
    
    
    labelCount.Caption = ListBox1.ListCount - 1
End Sub

Sub clearAll() 'to clear comboboxes except teams
    If Not bInit Then cmbName.Value = ""  ' ** doesn't run when calling from UserForm_Initialize **from Taller
    cmbDate.Clear
    cmbName.Clear
    'cmbName.Value = ""
    ListBox1.Clear
End Sub

Sub Team() 'for adding the teams dropdown in cmbTeam
    clearAll
    Dim ws As Worksheet, _
        Dic As Object, _
        rCell As Range, _
        Key
    
    Set ws = Worksheets("Sheet1")
    Set Dic = CreateObject("Scripting.Dictionary")
    
    
    For Each rCell In ws.Range("A2", ws.Cells(Rows.count, "A").End(xlUp))
            If Not Dic.exists(rCell.Value) And Not rCell = "" Then
                Dic.Add rCell.Value, Nothing
            End If
    Next rCell
    
    For Each Key In Dic
        cmbTeam.AddItem Key
    Next
End Sub

Sub forNamesCombobox() 'for adding the names dropdown in cmbName
Dim ws As Worksheet, _
    Dic As Object, _
    rCell As Range, _
    Key

Set ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")


For Each rCell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
        If Not Dic.exists(rCell.Value) And rCell.Offset(0, -1) = cmbTeam.Value Then
            Dic.Add rCell.Value, Nothing
        End If
Next rCell

For Each Key In Dic
    cmbName.AddItem Key
Next
End Sub

Sub forDateCombobox() 'for adding the date dropdown in cmbDate
            Dim date1 As Variant
            Dim date2 As Variant
            date1 = Format(Now, "mmmm yyyy")
            date2 = Format(DateAdd("m", -1, CDate(date1)), "mmmm yyyy")
            
            With cmbDate
            .Clear
            .AddItem Format(date2, "mmmm yyyy")
            .AddItem Format(date1, "mmmm yyyy")
            .Value = Format(date1, "mmmm yyyy")
            End With
            
End Sub

工作表1

一个 B
团队 名字
鹦鹉 莉娜
乔治 诺里
乔治 最大限度
杰克 担

工作表2

一个 B 碳
日期 ID 名字
2025年3月25日 1101 莉娜
2025年4月25日 1102 莉娜
2025年3月25日 1103 诺里
2025年4月25日 1104 诺里
2025年3月25日 1105 担
2025年4月25日 1106 担

现在,在团队组合框的更改事件期间,我希望清除名称组合框(“Nory”或任何值cmbName都应被删除并消失)。

从上面的代码中,团队变更事件片段是:

Private Sub cmbTeam_Change()
    cmbDate.Value = ""
    'cmbName.Value = "" 'issue, during initialize, default cmbname is removed.
    clearAll 'used this instead
    forDateCombobox 'fills in date dropdowns
End Sub

Sub clearAll() 'to clear comboboxes except teams
    If Not bInit Then cmbName.Value = ""  ' ** doesn't run when calling from UserForm_Initialize **from Taller
    cmbDate.Clear
    cmbName.Clear
    'cmbName.Value = ""
    ListBox1.Clear
End Sub

即使我将在初始化期间插入cmbName.Value = ""到 sub 中clearAll,"Nory"也会被删除,但我不希望它在初始化时被删除。

如何修复上述代码,在初始化期间,“Nory”将保留,团队和日期也将保留,而当团队发生变化时,日期和姓名组合框将为空白。

非常感谢您的帮助。

excel
  • 1 1 个回答
  • 89 Views

1 个回答

  • Voted
  1. Best Answer
    taller
    2025-04-27T05:53:40+08:002025-04-27T05:53:40+08:00

    引入模块范围变量可防止在初始化事件代码运行时清除 cmbName。

    如果脚本没有按预期运行,请更新您的原始帖子以阐明清除组合框值和/或其下拉列表的逻辑。

    Option Explicit
    Dim bInit As Boolean ' ** module-scoped variable 
    
    
    Private Sub UserForm_Initialize()
        bInit = True  ' ** set UserForm_Initialize mode
        clearAll
        Me.cmbName.Value = "Nory"
        Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
        Dim i As Long
        Dim arr: arr = ws.Range("B1").CurrentRegion.Value
        Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
        
        For i = 2 To UBound(arr)
            dict(arr(i, 2)) = arr(i, 1)
        Next
        ' ***
        If dict.exists(Me.cmbName.Value) Then
            Me.cmbTeam.Value = dict(Me.cmbName.Value)
            If Not cmbTeam.Value = "" And Not cmbName.Value = "" Then
                Team
                forListBoxUpdate
                forDateCombobox
                forNamesCombobox
            Else
                Team
            End If
        Else
            Team
        End If
        bInit = False  ' ** reset
    End Sub
    
    Sub clearAll()
        cmbDate.Clear
        cmbName.Clear
        If Not bInit Then cmbName.Value = ""  ' ** doesn't clear cmbName when called by UserForm_Initialize
        ListBox1.Clear
    End Sub
    
    • 1

相关问题

  • 如何返回列出的合同上有费率但系统中没有费率的特定行?

  • 当某些值重复时自动在表中添加参考字段?

  • 循环遍历具有更改单元格地址的列

  • 搜索字符串并输出与该字符串对应的值

  • Excel中有没有一种方法可以计算字符串中特定文本的出现次数,但也包括前一个字符?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve