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界面截图:
内容的提问来源于stack exchange,提问作者OscarV
相关产品推荐
相关产品推荐

