Vba 在单元格中输入时间和日期

Vba 在单元格中输入时间和日期,vba,excel,Vba,Excel,我正在使用条形码阅读器在一个单元格中输入序列号,然后使用以下命令在接下来的两个单元格中自动添加日期和时间: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count > 5 Then Exit Sub If Target.Column <> 5 Then Exit Sub Application.EnableEvents = False Target(1, 2).Value = Date

我正在使用条形码阅读器在一个单元格中输入序列号,然后使用以下命令在接下来的两个单元格中自动添加日期和时间:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 5 Then Exit Sub
If Target.Column <> 5 Then Exit Sub
Application.EnableEvents = False
Target(1, 2).Value = Date
Target(1, 3).Value = Time
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果Target.Cells.Count>5,则退出Sub
如果目标为第5列,则退出子列
Application.EnableEvents=False
目标(1,2)。值=日期
目标(1,3)。值=时间
Application.EnableEvents=True
端接头

我希望能够扫描条形码,或者自动找到序列号并在两个“其他单元格4和5”中放置日期时间戳,或者如果序列号不在单元格中,则将其放置在该单元格中并将日期时间放置在2和3单元格中。

我确定您的条形码阅读器是“T”型的在键盘电缆中,也就是说,向电脑发送按键。只要Excel光标位于工作表的第5列(第2个退出条件),VBA代码就会在每次放炮后在光标右侧的1和2单元格(即F..G列)中添加日期和时间

如果要将日期和时间定位到工作表的第2和第3列(即B.C列),而不考虑触发工作表更改触发器时光标的位置,则应使用此代码

Target.EntireRow.Cells(1, 2).Value = Date
Target.EntireRow.Cells(1, 3).Value = Time
现在。。。在另一个Excel表格中查找捕获的条形码并找到匹配的序列号可以像VLOOKUP函数一样简单,您可以自动粘贴到日期旁边的单元格中,也可以使用Do。。。虽然用于扫描(命名)范围的循环构造:

专用函数SernoByBarcode(条形码作为字符串)作为字符串
作为范围的Dim DBase,作为长度的Idx
Set DBase=Range(“数据库”)'命名范围“数据库”在第1列中包含条形码,在第2列中包含序列号
Idx=2'第一行包含标题
SernoByBarcode=“#未找到”默认返回值
在DBase(Idx,1)“中断第一条空记录时执行此操作
如果数据库(Idx,1)=条形码,则
SernoByBarcode=DBase(Idx,2)
退出功能
如果结束
Idx=Idx+1
环
端函数
如果在设置日期和时间之前调用函数SernoByBarcode,则可以使用多个If语句来确定输出格式(即includeng/excluding Serial Number)

编辑

条形码是否始终在同一单元格中扫描(我选择B2) 从第5列开始扫描列表中是否存在条形码。。。如果是,则写入8/9/10,否则写入5/6/7。。。只需稍加修改即可使用Find函数,现在返回行索引而不是字符串值

Private Sub Worksheet_Change(ByVal Target As Range)
' Barcode always scanned into cell B2
' if barcode found in column 5, fill column 8,9,10 else fill columns 5,6,7
' row 1, columns 5..10 contain column headers
Dim Idx As Long

    If Target.Row = 2 And Target.Column = 2 Then
        Application.EnableEvents = False
        Idx = FindBarcode(Target.Value)

        If Me.Cells(Idx, 5) = "" Then
            Me.Cells(Idx, 5) = Target.Value
            Me.Cells(Idx, 6) = Date
            Me.Cells(Idx, 7) = Time
        Else
            Me.Cells(Idx, 8) = Target.Value
            Me.Cells(Idx, 9) = Date
            Me.Cells(Idx, 10) = Time
        End If

        ' keep cursor in Scan field
        Me.Cells(2, 2).Select
        Application.EnableEvents = True
    End If

End Sub

Private Function FindBarcode(Barcode As String) As Long
Dim DBase As Range, Idx As Long

    Set DBase = ActiveSheet.[E1] ' start of table
    Idx = 2 'first row contains headers

    Do While DBase(Idx, 1) <> "" ' break on 1st empty record
        If DBase(Idx, 1) = Barcode Then
            Exit Do
        End If
        Idx = Idx + 1
    Loop
    FindBarcode = Idx
End Function
Private子工作表\u更改(ByVal目标作为范围)
'条形码始终扫描到单元格B2中
'如果在第5列中找到条形码,请填写第8、9、10列,否则请填写第5、6、7列
'第1行第5列..10列包含列标题
变暗Idx为长
如果Target.Row=2,Target.Column=2,则
Application.EnableEvents=False
Idx=FindBarcode(Target.Value)
如果Me.Cells(Idx,5)=“那么
Me.Cells(Idx,5)=目标值
Me.Cells(Idx,6)=日期
Me.Cells(Idx,7)=时间
其他的
Me.Cells(Idx,8)=目标值
Me.Cells(Idx,9)=日期
Me.Cells(Idx,10)=时间
如果结束
'将光标保持在扫描字段中
Me.Cells(2,2)。选择
Application.EnableEvents=True
如果结束
端接头
专用函数FindBarcode(条形码为字符串)长度为
作为范围的Dim DBase,作为长度的Idx
设置DBase=ActiveSheet。[E1]'表的开头
Idx=2'第一行包含标题
在DBase(Idx,1)“中断第一条空记录时执行此操作
如果数据库(Idx,1)=条形码,则
退出Do
如果结束
Idx=Idx+1
环
FindBarcode=Idx
端函数

序列号应该在哪里,而不是在单元格中?我不明白这个问题。您是将序列号打印到工作表上还是只是查找?如果您将序列号打印到
工作表\u Change
之外的工作表中,为什么不同时添加日期和时间戳呢?条形码读取器是usb//游标位于单元格第5列第2行,扫描序列号。序列号位于第5列第2行日期位于第6列第2行,时间位于第7列第2行。“如果”我扫描文件中的相同序列号,并填写第2行第5-7列,我想将相同序列号放在第8列第2行,日期列第9列第2行,时间列第10列第2行。。使用此功能将气体阅读器签出给客户,然后在一天结束时将其签入OK。。。现在有道理了。。。参见编辑
Private Function SernoByBarcode(Barcode As String) As String
Dim DBase As Range, Idx As Long

    Set DBase = Range("Database") ' named range "Database" contains Barcode in column1, SerNo in column2
    Idx = 2 'first row contains headers
    SernoByBarcode = "#NOT/FOUND" ' default return value

    Do While DBase(Idx, 1) <> "" ' break on 1st empty record
        If DBase(Idx, 1) = Barcode Then
            SernoByBarcode = DBase(Idx, 2)
            Exit Function
        End If
        Idx = Idx + 1
    Loop
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
' Barcode always scanned into cell B2
' if barcode found in column 5, fill column 8,9,10 else fill columns 5,6,7
' row 1, columns 5..10 contain column headers
Dim Idx As Long

    If Target.Row = 2 And Target.Column = 2 Then
        Application.EnableEvents = False
        Idx = FindBarcode(Target.Value)

        If Me.Cells(Idx, 5) = "" Then
            Me.Cells(Idx, 5) = Target.Value
            Me.Cells(Idx, 6) = Date
            Me.Cells(Idx, 7) = Time
        Else
            Me.Cells(Idx, 8) = Target.Value
            Me.Cells(Idx, 9) = Date
            Me.Cells(Idx, 10) = Time
        End If

        ' keep cursor in Scan field
        Me.Cells(2, 2).Select
        Application.EnableEvents = True
    End If

End Sub

Private Function FindBarcode(Barcode As String) As Long
Dim DBase As Range, Idx As Long

    Set DBase = ActiveSheet.[E1] ' start of table
    Idx = 2 'first row contains headers

    Do While DBase(Idx, 1) <> "" ' break on 1st empty record
        If DBase(Idx, 1) = Barcode Then
            Exit Do
        End If
        Idx = Idx + 1
    Loop
    FindBarcode = Idx
End Function