运行条形码跟踪器以跟踪使用 Zebra 扫描仪扫描条形码时的进出时间。在宏模块上,我在扫描条形码时遇到溢出 6 错误。条形码以永无止境的数字符号形式出现,但当单击单元格本身时,会显示 12 位条形码编号。
我要调试的代码行是:
barcode = Worksheets("Sheet1").Cells(2, 2)
这是完整的代码:
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
barcode = Worksheets("Sheet1").Cells(2, 2)
If barcode <> "" Then
Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlnext, MatchCase=False, SearchFormat:=False
If rng Is Nothing Then
again:
ActiveSheet.Columns("a:a").Find("").Select
ActiveCell.Value = barcode
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Date & " " & Time
ActiveCell. NumberFormat = "m/d/yyyy h:mm AM?PM"
Worksheets("Sheet1".Cells(2,2) = ""
Else
rownumber = rng.Row
Sheet1.Cells(rownumber, 3). Select
ActiveCell.Interiot.ColorIndex = 4
If Worksheets("Sheet1").Cells(rownumber, 3).Value <> "" Then GoTo
again
Worksheets("Sheet1").Cells(rownumber, 1).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat - "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
End If
End If
End Sub
请问有人知道如何解决这个问题吗?
尝试扫描条形码以输入进出时间。条形码作为永无止境的数字符号出现,带有
溢出6错误
在 VBA 中。
试试这个 - 内嵌评论:
Sub inout()
Dim barcode As String, rng As Range, f As Range, ws As Worksheet, newrow As Boolean
Set ws = Sheet1
barcode = Trim(ws.Cells(2, 2).Value)
If barcode <> "" Then
Set rng = ws.Columns(1) 'column with barcodes
'find the *last* instance of the barcode in ColA (if it exists)
Set f = rng.Find(What:=barcode, LookIn:=xlFormulas, LookAt:=xlWhole, _
after:=rng.Cells(1), MatchCase:=False, SearchDirection:=xlPrevious)
newrow = f Is Nothing 'need a new row if barcode not found
If Not newrow Then newrow = Len(f.EntireRow.Cells(3).Value) > 0 '...or if found but "in" is already populated
If newrow Then 'add new row?
Set f = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1)
f.Value = barcode
End If
TimeStamp f.Offset(0, IIf(newrow, 1, 2)) 'add "out" or "in" timestamp
ws.Cells(2, 2).ClearContents
End If
End Sub
'format a cell and add a timestamp
Sub TimeStamp(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub