You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA UserForm扫码时文本框不新增、产品信息不更新问题求助

问题:UserForm扫码功能异常:重复扫码不更新信息、无法新增输入框

我正在开发一个UserForm,用来展示从「Products」工作表获取的条形码、产品名称和价格信息。首次扫码时产品信息能正常显示,但第二次扫码时产品名称和价格标签不会更新,第三次扫码时无法新增TextBox。

相关VBA代码如下:

Private Sub BarcodeTextBox_Change()
    Dim scannedCode As String
    Dim foundProduct As Range
    
    ' Get the scanned barcode from the TextBox
    scannedCode = BarcodeTextBox.Text
    
    ' Search for the scanned product code in the "Products" worksheet
    Set foundProduct = Worksheets("Products").Columns(1).Find(What:=scannedCode, LookIn:=xlValues, LookAt:=xlWhole)
    
    If Not foundProduct Is Nothing Then
        ' Product found, retrieve information
        Dim productName As String
        Dim productPrice As Double
        
        ' Retrieve product name and price from adjacent cells
        productName = foundProduct.Offset(0, 1).Value
        productPrice = foundProduct.Offset(0, 2).Value
        
        ' Update labels to display product information
        ProductNameLabel.Caption = "Product Name: " & productName
        ProductPriceLabel.Caption = "Price: $" & Format(productPrice, "0.00")
        
        ' Add a new TextBox below BarcodeTextBox for continuous scanning
        AddNewBarcodeTextBox
        SendKeys "{TAB}", False
    Else
        ' Product not found, clear labels
        ProductNameLabel.Caption = ""
        ProductPriceLabel.Caption = ""
    End If
End Sub

Private Sub AddNewBarcodeTextBox()
    ' Add a new TextBox below BarcodeTextBox
    Dim newTextBox As MSForms.TextBox
    Set newTextBox = Me.Controls.Add("Forms.TextBox.1")
    
    With newTextBox
        .Top = BarcodeTextBox.Top + BarcodeTextBox.Height + 5 ' Position below the BarcodeTextBox
        .Left = BarcodeTextBox.Left
        .Width = BarcodeTextBox.Width
        .Height = BarcodeTextBox.Height
        .Name = "NewBarcodeTextBox"
        .TabStop = True
        .SetFocus
    End With
End Sub

UserForm界面截图:
UserForm界面截图

内容的提问来源于stack exchange,提问作者OscarV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:22:41