VBA宏在某列发生更改后未能自动运行

huangapple go评论67阅读模式
英文:

VBA Macro failed to run automatically after changes happening in certain column

问题

我正在经历一个延迟或需要额外点击才能在输入列E中的单元格值后运行宏的问题。我已经使用Private Sub Worksheet_SelectionChange来触发宏。然而,实际行为是在按下Enter键后,我仍然需要点击单元格才能执行宏。我不确定为什么会出现这种情况。是否有人能够提供见解或建议来解决这个问题?

我尝试使用"If Not Intersect"方法以及"Application.EnableEvents"技术来触发我的宏,但它并没有按预期工作。尽管实施了这些更改,但宏仍然无法执行。我已经没有关于如何解决这个问题的想法了。是否有人能够提供关于为什么这些方法不起作用以及建议实现所需功能的替代方法的见解?

基本上,这是VBA中的代码,我正在获取列E中的值,一旦它与列A中的值相同,它将为该特定行填充时间戳。

Sub recordTimeStamp()
    ' 声明变量
    Dim Barcode As String
    Dim CartNo As String
    Dim Row As Long
    Dim Timestamp As String

    ' 从扫描仪获取条形码
    Barcode = ActiveSheet.Range("E" & Application.ActiveCell.Row).Value

    ' 获取当前时间戳
    Timestamp = Format(Now, "yyyy-MM-dd hh:mm AM/PM")

    ' 循环遍历列A中的所有行
    For Row = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        ' 如果条形码与行中的购物车号匹配
        If Barcode = ActiveSheet.Cells(Row, 1).Value Then
            ' 如果列E的特定行不为空,填写列E,否则填写列F
            If IsEmpty(ActiveSheet.Cells(Row, 5).Value) Then
                ' 在列E中填写时间戳
                ActiveSheet.Cells(Row, 5).Value = Timestamp
            Else
                ' 在列F中填写时间戳
                ActiveSheet.Cells(Row, 6) = Timestamp
            End If
            ' 清除列E中动态(选定)行的值
            ActiveCell.ClearContents
            ' 选择带有时间戳的行
            ActiveSheet.Cells(Row, 1).Select
            ' 退出循环
            Exit For
        End If
    Next Row
End Sub

这是我调用它的地方:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    recordTimeStamp
End Sub
英文:

I'm experiencing a delay or requiring an extra click for my macro to run after entering a value in a cell within column E. I have implemented the macro using Private Sub Worksheet_SelectionChange to trigger it. However, the actual behavior is that after pressing Enter, I still need to click back on the cell for the macro to execute. I'm unsure why this is happening. Can anyone provide insights or suggestions to resolve this issue?

have attempted to use the "If Not Intersect" method along with the "Application.EnableEvents" technique to trigger my macro, but it is not working as expected. Despite implementing these changes, the macro fails to execute. I have run out of ideas on how to resolve this issue. Can someone provide insights into why these methods are not working and suggest alternative approaches to achieve the desired functionality?

Basically this is the code in VBA, I am getting the value in column E, once it is having the same value as column A, it will fill in timestamp for that particular row.

Sub recordTimeStamp()

' Declare variables
Dim Barcode As String
Dim CartNo As String
Dim Row As Long
Dim Timestamp As String

' Get the barcode from the scanner
Barcode = ActiveSheet.Range("E" & Application.ActiveCell.Row).Value

' Get the current timestamp
Timestamp = Format(Now, "yyyy-MM-dd hh:mm AM/PM")

' Loop through all the rows in column A
For Row = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

' If the barcode matches the cart number in a row
If Barcode = ActiveSheet.Cells(Row, 1).Value Then
    
    ' If the particular row of column E is not empty, fill in 
column E else column F
    If IsEmpty(ActiveSheet.Cells(Row, 5).Value) Then
        ' Fill in the timestamp in column E
        ActiveSheet.Cells(Row, 5).Value = Timestamp
    Else
        ' Fill in the timestamp in column F
        ActiveSheet.Cells(Row, 6) = Timestamp
    
    End If
    
    ' Remove the value in the dynamic (selected) row in column E
    ActiveCell.ClearContents
    
    ' Select the row with the timestamp
    ActiveSheet.Cells(Row, 1).Select
    
    ' Exit the loop
    Exit For

End If

Next Row
End Sub

This is where I call it:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    recordTimeStamp

End Sub

答案1

得分: 1

我猜你应该使用Change事件,而不是SelectionChange(Worksheet_Change)。

例如,当你在单元格E1中输入内容并按Tab键时,焦点将设置为F1。Change事件的target将设置为E1(因为单元格E1已更改)。在这个触发器中,你可以对这个单元格的更改值做出反应。

在此事件之后,将触发SelectionChange事件,但此时target将变为F1,因为现在此单元格具有焦点。在这个触发器中,你既不知道焦点之前是哪个单元格,也不知道是否在该单元格中进行了更改。

英文:

I guess you should use the Change-Event, not SelectionChange (Worksheet_Change).

For example, when you enter something in Cell E1 and press Tab, Focus will be set to F1. The Change-Event will have target set to E1 (because cell E1 was changed). In this trigger you can react on the changed value of this cell.

After this event, the SelectionChange-Event will be triggered, but target will be F1 because now this cell has the focus. In this trigger, you neither know which cell had the focus before nor if or if not something was changed in that cell.

huangapple
  • 本文由 发表于 2023年5月25日 17:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定