excel 检查单元格是否正在被编辑,或者用户是否正在Formua栏中键入内容

j91ykkif  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(116)

所以这个想法是:
我有一个宏说(用于演示目的):

Sub test_routine()

    Dim cell As Range
    
    Range("test_range").ClearContents
    
    For Each cell In Range("test_range")
        DoEvents
        Application.Wait Now + TimeValue("00:00:01")
        cell.Value = "input"
    Next
    
End Sub

字符串
当我尝试编辑单元格或选择编辑栏时,宏停止工作。
有没有一种方法可以在用户输入内容时停止宏的执行?
如果我不双击单元格就直接开始输入(在单击单元格或在公式栏中),这不起作用,也不能解决问题:

Public cell_active As Range

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Do While Target = cell_active
        DoEvents
        Application.Wait Now + TimeValue("00:00:01")
    Loop
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Set cell_active = Target
End Sub

im9ewurl

im9ewurl1#

你可以尝试使用OnTime。举例来说:

Sub start()
    update
End Sub

Sub update()
    Static cellNum As Long 'track value between calls
    
    With ThisWorkbook.Sheets("Test").Range("A1:A10")
        If cellNum = 0 Then  'starting off...
            .Value = ""
            cellNum = 1
        End If
        
        .Cells(cellNum).Value = Now
        If cellNum < .Cells.Count Then
            cellNum = cellNum + 1
            'schedule the next run
            Application.OnTime Now + TimeSerial(0, 0, 2), "update"
        Else
            cellNum = 0 'at end of range: reset
        End If
    End With
End Sub

字符串
如果您运行start,然后进入编辑模式,更新将暂停,直到您退出编辑模式。

相关问题