我在实现新的 VBA 代码时遇到问题。我对这种编码形式真的很生疏,并试图创建一些新的解决方案。
我有 3 张。我使用条形码扫描仪来扫描代码的一个(最终将成为一个界面)。 第二张纸用作库存。 一张作为销售记录的三张纸。
当我扫描条形码时,如果库存中不存在该条形码,则会显示错误消息。 我还想检查销售表的 A 列。如果条形码存在,则应显示错误消息。 如果不存在,则它应该随程序一起携带。 我已经设法一路到达最后的状态网,但我无法将这两个代码集成在一起。
表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库存上的数据从未售出更改为已售出(或从库存中删除),但无法解决此问题问题,我有点沮丧。
任何帮助将不胜感激。
所以过了一段时间我最终找到了解决方案。我会将其发布,以防对其他人有所帮助。
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