【问题标题】:Excel and Visual Basic Barcode In/Out checkout systemExcel 和 Visual Basic 条码输入/输出结帐系统
【发布时间】:2022-01-05 22:59:13
【问题描述】:

我正在使用 Visual Basic 在 Excel 工作表中创建结帐系统。该表将填写一个项目的信息,每个项目都需要我们发送一个工具包。此 Excel 表将允许扫描条形码,当发生这种情况时,它会检查放置“退出”时间。当再次扫描该条形码时,它会放置一个“进入”时间。我遇到的问题是,如果第三次扫描该条形码,它只会更新超时时间。

如何设置它会看到记录了“输入”和“输出”时间,因此进入该行中的下一个空白单元格并添加条形码 + 新的“输入”或“输出”时间.任何帮助将不胜感激!

这是我正在使用的代码。

工作表上的代码

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
        Application.EnableEvents = False
        Call inout
        Application.EnableEvents = True
    End If

End Sub

宏代码

Sub inout()
    Dim barcode As String
    Dim rng As Range
    Dim rownumber As Long

    barcode = Worksheets("Sheet1").Cells(2, 2)

    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
        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
        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
    Worksheets("Sheet1").Cells(2, 2).Select

    
End Sub

【问题讨论】:

    标签: excel vba timestamp barcode


    【解决方案1】:

    所有这些都在工作表代码模块中:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
            inout 'use of Call is deprecated
        End If
    End Sub
    
    
    Sub inout()
        Dim barcode As String
        Dim rng As Range
        Dim newRow As Boolean
    
        barcode = Me.Cells(2, 2)
    
        'find the *last* instance of `barcode` in ColA
        Set rng = Me.Columns("A").Find(What:=barcode, after:=Me.Range("A1"), _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
        
        'figure out if we need to add a new row, or update an existing one
        If rng Is Nothing Then
            newRow = True 'no matching barcode
        Else
            'does the last match already have an "in" timestamp?
            If Len(rng.Offset(0, 2).Value) > 0 Then newRow = True
        End If
        
        If newRow Then
            Set rng = Me.Cells(Me.Rows.Count, "A").End(xlUp).Offset(1, 0)
            rng.Value = barcode
            SetTime rng.Offset(0, 1) 'new row, so set "out"
        Else
            SetTime rng.Offset(0, 2) 'existing row so set "in"
        End If
        
        Me.Cells(2, 2).Select
    End Sub
    
    'set cell numberformat and set value to current time
    Sub SetTime(c As Range)
        With c
            .NumberFormat = "m/d/yyyy h:mm AM/PM"
            .Value = Now
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多