用于 RFID 读取器的 VBA - 如何捕获特定列中下一个空单元格中的 Tag_ID(例如从 A2 到 A3000)

问题描述 投票:0回答:1

The excel Speadsheet to capture TAG_ID in Column A

我试图在 RFID 读卡器上每次点击卡时捕获 Tag_ID ONLY A 列。 时间将自动输出在 B 列中,对应于 A 列中的每个 TAG_ID 捕获。

问题来了:

在允许任何人在读卡器上敲击卡之前,我必须故意将光标放在 A2 上。 如果光标放置在电子表格的其他位置(例如光标位于 H3),则 Tag_ID 会在 H3 处捕获,但不会在 A2 处捕获。

如果有人可以帮助我调整我的VBA代码,这样无论我将光标放在电子表格上的哪个位置,TAG_ID都会被捕获在A2中(在开头),然后对于后续的卡片点击,Tag_ID将是在 A 列的下一个空行中捕获。

这是当前用于捕获 TAG_ID 的 VBA 代码:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Cells(Target.Row, 2).Value = Now
Beep
End If

End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

谢谢你

在理解和编写VBA代码方面我还是个新手。

我尝试过其他类似的代码来测试:

Sub Store_Reference_Next_Empty_Row()

Dim nextEmptyCell As Range

Set nextEmptyCell= Range("A" & Row.Count).End(xlUp).Offset(1)

nextEmptyCell.value=Now

用这个代码 当我运行宏时,数据(错误)出现在表格的最后一行。

enter image description here

如果使用卡片点击RFID阅读器,我必须故意将光标放在A2上...但即使捕获了TAG_ID,也没有输出时钟

enter image description here

excel vba rfid
1个回答
0
投票

选项 1: 假设A列的数据是连续的...

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        If Target.Column = 1 And (Target.Value) > 0 Then
            Application.EnableEvents = False
            Cells(Target.Row, 2).Value = Now
            Beep
            Dim c As Range
            ' Locate the last row from top
            Set c = Target.Offset(1)
            If c.ListObject Is Nothing Then
                ' the table is full, add a row
                Target.ListObject.ListRows.Add
            End If
            c.Select
            Application.EnableEvents = True
        End If
    End If
End Sub

选项2: 不管A列的数据是否连续...

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        If Target.Column = 1 And (Target.Value) > 0 Then
            Application.EnableEvents = False
            Cells(Target.Row, 2).Value = Now
            Beep
            Dim c As Range
            ' Locate the last row of table from bottom
            Set c = Me.Cells(Me.Rows.Count, 1).End(xlUp)
            If c.Value = "" Then
                ' locate "real" last data row
                Set c = c.End(xlUp)
            Else
                ' the table is full, add a row
                c.ListObject.ListRows.Add
            End If
            c.Offset(1).Select
            Application.EnableEvents = True
        End If
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.