提问人:Alfonso Gomez-Jordana Martin 提问时间:10/26/2023 最后编辑:Alfonso Gomez-Jordana Martin 更新时间:10/26/2023 访问量:32
在销售控制的 VBA 编码上实施检查和擦除时出现的问题
Issues implementing a check and erase on VBA coding for sells control
问:
我在实现新的VBA代码时遇到了问题。我对这种编码形式非常生疏,并试图创建一些新的解决方案。
我有 3 张。一个(最终将成为界面),我使用条形码扫描仪扫描代码。 用作库存的第二张工作表。 用作销售唱片的 thris 表。
当我扫描条形码时,如果库存中不存在条形码,则会显示一条错误消息。 我还想检查销售表,A 列。如果条形码存在,则应显示错误消息。 如果 if 不存在,则应随程序一起携带。 我已经设法一直到达最后一个statemnet,但我没有设法将这两个代码集成在一起。
表 1 中的代码,扫描
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C2")) Is Nothing Then
Call receive
Application.EnableEvents = True
End If
End Sub
宏 1 中的代码
Sub receive()
Dim barcode As String
Dim rng As Range
Dim rown, lrow As Long
Dim qty As Long
barcode = Tabelle1.Cells(2, 3)
Tabelle2.Activate
'is there a barcode
If barcode = "" Then Exit Sub
If barcode <> "" Then
Set rng = Tabelle2.Columns("A:A").Find(what:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' send an error message if you do not find it
If rng Is Nothing Then
MsgBox "Barcode nicht gefunden"
GoTo ende
Else
'determine which row has the barcode
rown = rng.Row
If barcode = "" Then Exit Sub
If barcode <> "" Then
'add the value to the columns
Tabelle2.Cells(rown, 8).Value = Tabelle2.Cells(rown, 8).Value
'copy the description information
Tabelle2.Range(Cells(rown, 2), Cells(rown, 7)).Copy
Tabelle3.Activate
'paste it on the lastrow of the scan sheet
lrow = Tabelle3.Cells(Rows.Count, 2).End(xlUp).Row + 1
Tabelle3.Cells(lrow, 2).PasteSpecial
'enter the barcode and the barcode information
Tabelle3.Cells(lrow, 1).Value = barcode
'enter the date and time for when this happened
Tabelle3.Cells(lrow, 8) = Date & " " & Time
Tabelle3.Cells(lrow, 8).NumberFormat = "m/d/yyyy h:mm"
MsgBox "Registered"
GoTo ende
End If
End If
End If
ende:
'turn off the marching ants
Application.CutCopyMode = False
Tabelle1.Activate
Tabelle1.Cells(2, 3).ClearContents
ActiveWorkbook.Sheets("scan").Activate
Sheets("scan").Range("C2").Select '(and activate)
End Sub
代码宏 2
Sub TestForDuplicates()
Dim rng As Range
'Store Range to search through
Set rng = Range("A2:A3")
'Test Range for Duplicates
If Evaluate(Replace("NOT(AND((COUNTIF(@,@)=1)))", "@", rng.Address)) = True Then
MsgBox "Product already sold"
End Sub
我在将宏 2 的协议实现到宏 1 中时遇到了问题,因此,如果首先检查代码是否在清单中,如果为真,它会检查代码是否不在表 3 的 A 列上。如果这是真的,我希望它继续复制粘贴库存中的数据。
我想到的另一件事是,在工作表 3 销售中注册数据后,工作表 1 库存上的数据更改为未售出(或从库存中删除),但在无法解决这个问题后,我有点气馁。
任何帮助将不胜感激。
答:
0赞
Alfonso Gomez-Jordana Martin
10/26/2023
#1
所以过了一会儿,我终于找到了解决方案。我会发布它,以防它可以帮助其他人。
Sub receive()
Dim barcode As String
Dim rng As Range
Dim rng3 As Range
Dim rown, lrow As Long
Dim qty As Long
barcode = Tabelle1.Cells(2, 3)
Tabelle2.Activate
'is there a barcode?
If barcode = "" Then Exit Sub
If barcode <> "" Then
Set rng = Tabelle2.Columns("A:A").Find(what:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' send an error message if you do not find it
If rng Is Nothing Then
MsgBox "Barcode nicht gefunden"
' send an error message if you do not find it
Else
Set rng3 = Tabelle3.Columns("A:A").Find(what:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' If found, Send error message
If Not rng3 Is Nothing Then
MsgBox "Bereits verkauftes Produkt"
'determine which row has the barcode
Else
rown = rng.Row
If barcode = "" Then Exit Sub
If barcode <> "" Then
'add the value to the columns
Tabelle2.Cells(rown, 8).Value = Tabelle2.Cells(rown, 8).Value
'copy the description information
Tabelle2.Range(Cells(rown, 2), Cells(rown, 7)).Copy
Tabelle3.Activate
'paste it on the last row of the sells sheet
lrow = Tabelle3.Cells(Rows.Count, 2).End(xlUp).Row + 1
Tabelle3.Cells(lrow, 2).PasteSpecial
'enter the barcode and the barcode information
Tabelle3.Cells(lrow, 1).Value = barcode
'enter the date and time for when this happened
Tabelle3.Cells(lrow, 8) = Date & " " & Time
Tabelle3.Cells(lrow, 8).NumberFormat = "m/d/yyyy h:mm"
MsgBox "Registered"
End If
End If
End If
End If
'turn off the marching ants
Application.CutCopyMode = False
Tabelle1.Activate
Tabelle1.Cells(2, 3).ClearContents
ActiveWorkbook.Sheets("scan").Activate
Sheets("scan").Range("C2").Select '(and activate)
End Sub
评论
Dim rown, lrow As Long
rown
Dim rown As Long, lrow As Long