判断Excel单元格是否处于编辑状态

判断Excel单元格是否处于编辑状态

以下是平时收集到的一个判断表格单元格是否处理编辑状态的代码,记不住是从哪弄的,转给大家,供学习参考。

'//取得类名
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'//设置钩子函数
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
'//结束钩子
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
'//下一个钩子
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As Long
'//取得当前线程的ID
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long

Public Const HCBT_SETFOCUS = 9
Public Const WH_CBT = 5
Public IHook As Long
Public IThreadId As Long
Public ClassName As String

'-------设置钩子-----------
Public Sub EnableHook()
    If IHook = 0 Then
        IThreadId = GetCurrentThreadId
        IHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, Application.Hinstance, IThreadId)
    End If
End Sub
'-------取消钩子-----------
Public Sub FreeHook()
    If IHook <> 0 Then
        Call UnhookWindowsHookEx(IHook)
        IHook = 0
    End If
End Sub
'---------回调----------------
Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
    If nCode < 0 Then
        HookProc = CallNextHookEx(IHook, nCode, wParam, lparam)
        Exit Function
    End If
    If nCode = HCBT_SETFOCUS Then
        ClassName = String(255, Chr(0))
        GetClassName wParam, ClassName, 255
        ClassName = Left(ClassName, InStr(ClassName, vbNullChar) - 1)
        If ClassName = "EXCEL<" Or ClassName = "EXCEL6" Then
            Application.StatusBar = "正处于编辑状态……"
        Else
            Application.StatusBar = False
        End If
    End If
    HookProc = CallNextHookEx(IHook, nCode, wParam, lparam)
End Function

如有侵权,请联系!

——专注办公软件的二次开发及培训,你有问题,我有思路!
——微博、微信、CSDN同号:w_dexu
——转载请注明出处!

微信二维码扫码加微信

你可能感兴趣的:(VBA,函数,表格,vba,excel,wps)