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 / 问题 / 79540362
Accepted
Shiela
Shiela
Asked: 2025-03-28 09:42:56 +0800 CST2025-03-28 09:42:56 +0800 CST 2025-03-28 09:42:56 +0800 CST

Excel VBA 中列表框更新后如何返回文本字段中的空白字段

  • 772

我这里有另一个表单,在初始化期间显示 Sheet1 的数据。当有 Listbox 项目选择或 Listbox afterupdate 事件时,列表框会从 Sheet2 返回文本字段中选定数字(以字母 D 表示)的值:

工作表1

一个 乙 碳 德
数字 日期1 日期2 版本
D-12300 2025 年 3 月 16 日 2025 年 3 月 16 日 1-50-02
D-12347 2025 年 3 月 17 日 2025 年 3 月 17 日 1-50-03
D-12348 2025 年 3 月 18 日 2025 年 3 月 18 日 1-50-04

工作表2

一个 乙 碳 德
数字 描述 日期 版本
D-12345 描述1 2025 年 2 月 15 日 1-50-01
D-12346 描述1 2025 年 3 月 16 日 1-50-02
D-12347 描述2 2025 年 3 月 17 日 1-50-03
D-12348 描述3 2025 年 3 月 18 日 1-50-04
D-12349 描述1 2025 年 3 月 19 日 1-50-05

列表框1

图片1

Private Sub ListBox1_AfterUpdate()
    Me.txtSheet1Digits.Value = ""
    Me.txtSheet1Date1.Value = ""
    Me.txtSheet1Date2.Value = ""
    Me.txtSheet1Version.Value = ""
    Me.txtSheet1Digits = ListBox1.Column(0)
    Me.txtSheet1Date1 = ListBox1.Column(1)
    Me.txtSheet1Date2 = ListBox1.Column(2)
    Me.txtSheet1Version = ListBox1.Column(3)
Matches
End Sub

上一篇文章中使用的代码片段:

Sub Matches()
    Dim ws2 As Worksheet: Set ws2 = Worksheets("Sheet2")
    Dim i As Long
    Dim arr: arr = ws2.Range("A1").CurrentRegion.Value
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim targetDate As Variant
    Dim targetVersion As Variant
    For i = 2 To UBound(arr)
        dict(arr(i, 1)) = Empty
    Next

    If dict.exists(Me.txtSheet1Digits.Value) Then
        targetDate = Application.VLookup(Me.txtSheet1Digits.Value, ws2.Range("B1").CurrentRegion.Value, 3, False)
        targetVersion = Application.VLookup(Me.txtSheet1Digits.Value, ws2.Range("B1").CurrentRegion.Value, 4, False)
        Me.txtSheet2Date = targetDate
        Me.txtSheet2Version = targetVersion
    End If
            If Not Me.txtSheet1Date1 = Me.txtSheet2Date Or _
            Not Me.txtSheet1Version = Me.txtSheet2Version Then
            labelSheet1Date1.ForeColor = vbRed
            txtSheet1Date1.ForeColor = vbRed
            
            labelSheet1Date2.ForeColor = vbRed
            txtSheet1Date2.ForeColor = vbRed
            
            labelSheet2Date.ForeColor = vbRed
            txtSheet2Date.ForeColor = vbRed
            
            labelSheet1Version.ForeColor = vbRed
            txtSheet1Version.ForeColor = vbRed
            
            labelSheet2Version.ForeColor = vbRed
            txtSheet2Version.ForeColor = vbRed
            Else
            labelSheet1Date1.ForeColor = vbBlack
            txtSheet1Date1.ForeColor = vbBlack
            
            labelSheet1Date2.ForeColor = vbBlack
            txtSheet1Date2.ForeColor = vbBlack
            
            labelSheet2Date.ForeColor = vbBlack
            txtSheet2Date.ForeColor = vbBlack
            
            labelSheet1Version.ForeColor = vbBlack
            txtSheet1Version.ForeColor = vbBlack
            
            labelSheet2Version.ForeColor = vbBlack
            txtSheet2Version.ForeColor = vbBlack
            End If   
End Sub

我只是想知道以下错误可能是什么:

当我单击列表框中的第一个项目时,它会读取第一个 If 条件,而 Sheet2 中没有任何匹配项,这是正确的,并将空白返回到两个文本字段。接下来,它读取第二个条件,此时所有条件都变为红色 - 正确。

图片2

当我单击列表框的第二项时,读数相同,并且现在转到第二个 If 条件的其他条件,因此它全是黑色 - 正确。

图片3

现在,当我再次单击返回到第一个项目时,它会读取相同的模式,但不会再向两个文本字段返回空白。带有箭头的两个文本字段继续具有先前选定项目的值 - 错误。它应该返回空白。

图片4

excel
  • 2 2 个回答
  • 33 Views

2 个回答

  • Voted
  1. Best Answer
    taller
    2025-03-28T10:41:53+08:002025-03-28T10:41:53+08:00

    如果 Sheet2 中没有匹配项,则添加Else子句来清除文本框。

        If dict.exists(Me.txtSheet1Digits.Value) Then
            targetDate = Application.VLookup(Me.txtSheet1Digits.Value, ws2.Range("B1").CurrentRegion.Value, 3, False)
            targetVersion = Application.VLookup(Me.txtSheet1Digits.Value, ws2.Range("B1").CurrentRegion.Value, 4, False)
            Me.txtSheet2Date = targetDate
            Me.txtSheet2Version = targetVersion
        Else 
            Me.txtSheet2Date = ""
            Me.txtSheet2Version = ""
        End If
    
    • 1
  2. Black cat
    2025-03-28T13:17:26+08:002025-03-28T13:17:26+08:00

    在事件中始终初始化两个文本框AfterUpdate。

    Private Sub ListBox1_AfterUpdate()
        Me.txtSheet1Digits.Value = ""
        Me.txtSheet1Date1.Value = ""
        Me.txtSheet1Date2.Value = ""
        Me.txtSheet1Version.Value = ""
        Me.txtSheet2Date.Value = ""   'added
        Me.txtSheet2Version.Value = ""    'added
        Me.txtSheet1Digits = ListBox1.Column(0)
        Me.txtSheet1Date1 = ListBox1.Column(1)
        Me.txtSheet1Date2 = ListBox1.Column(2)
        Me.txtSheet1Version = ListBox1.Column(3)
    Matches
    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