excel RFID阅读器的标签-如何在特定列中的下一个空单元格中捕获Tag_ID,比如从A2开始到A3000

lbsnaicq  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(126)

用于捕获A列中的TAG_ID的excel电子表格


的数据
我试图捕获的Tag_ID仅列A的RFID阅读器上的卡的每一个轻敲.时间将自动输出在列B对应于每个TAG_ID捕获列A.
这里的问题:
在允许任何人点击读卡器上的卡片之前,我必须故意将光标放在A2上。如果光标放在电子表格的其他位置(例如光标在H3),则Tag_ID在H3捕获,而不是在A2捕获。
我非常感谢如果任何人可以帮助我调整我的代码,这样无论我把光标放在电子表格上,TAG_ID将被捕获在A2(在开始),然后为后续卡水龙头,Tag_ID将被捕获在列A中的下一个空行。
下面是当前用于捕获TAG_ID的代码:

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

字符串
谢谢你
我在理解和编写JavaScript代码方面仍然是个新手。
我尝试了其他类似的代码来测试:

Sub Store_Reference_Next_Empty_Row()

Dim nextEmptyCell As Range

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

nextEmptyCell.value=Now


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



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


tvmytwxo

tvmytwxo1#

如果工作表中有一个表格是使用插入>表格或Ctrl+T创建的,那么定位最后一个数据行的方法略有不同。您不需要使用Worksheet_SelectionChange事件。

  • 选项1:* 假设A列中的数据是连续的(意味着数据之间没有空白单元格).
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        ' Taget is non-blank and on Column A
        If Target.Column = 1 And Len(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
        ' Taget is non-blank and on Column A
        If Target.Column = 1 And Len(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

  • 更新2:*

问:在任何时间点(如果在开始时必须选择A2),如果在捕获列A中的数据期间意外选择了其他单元格,则会破坏捕获列A中数据的连续性。

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        Dim tValue, c As Range
        tValue = Target.Value
        ' Assumes the length of TAG_ID is 7, modify as needed
        If Len(tValue) = 7 And VBA.IsNumeric(tValue) Then
            Application.EnableEvents = False
            ' Scan result is in column A
            If Target.Column = 1 Then
                Cells(Target.Row, 2).Value = Now
                Set c = Target
            Else
                ' Scan result is NOT in column A
                ' Locate the last row of table from bottom
                Set c = Me.Cells(Me.Rows.Count, 1).End(xlUp)
                If c.Value = "" Then Set c = c.End(xlUp)
                Set c = c.Offset(1)
                ' Clear TAG_ID
                Target.Value = ""
                ' Relocate to column A
                c.Value = tValue
                c.Offset(0, 1) = Now
            End If
            ' Select cell for next scan
            c.Offset(1).Select
            ' Expand table
            If ActiveCell.ListObject Is Nothing Then
                c.ListObject.ListRows.Add
            End If
            Beep
            Application.EnableEvents = True
        End If
    End If
End Sub


添加SelectionChange事件可确保活动单元格始终位于A列中的正确单元格上。这有助于防止新的TAG扫描破坏表中的现有数据。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim c As Range
    If Target.CountLarge = 1 Then
        If Me.ListObjects.Count > 0 Then
            Set c = Application.Intersect(Me.ListObjects(1).Range, Target)
            If Not c Is Nothing Then
                Set c = Me.Cells(Me.Rows.Count, 1).End(xlUp)
                If c.Value = "" Then Set c = c.End(xlUp)
                Set c = c.Offset(1)
                Application.EnableEvents = False
                c.Select
                Application.EnableEvents = True
            End If
        End If
    End If
End Sub

更新3

问:如果我有一个单元格范围(命名为“NoTouch”)不在表中,我不希望TAG_ID覆盖,但仍然跳转到捕获列A中的数据。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim c As Range, dataRange As Range
    If Target.CountLarge = 1 Then
        If Me.ListObjects.Count > 0 Then
            Set dataRange = Application.Union(Me.ListObjects(1).Range, Me.Range("NoTouch"))
            Set c = Application.Intersect(dataRange, Target)
            If Not c Is Nothing Then
                Set c = Me.Cells(Me.Rows.Count, 1).End(xlUp)
                If c.Value = "" Then Set c = c.End(xlUp)
                Set c = c.Offset(1)
                Application.EnableEvents = False
                c.Select
                Application.EnableEvents = True
            End If
        End If
    End If
End Sub

相关问题