【问题标题】:Excel VBA: "Compile Error: Wrong Number of arguments or invalid property assignment"Excel VBA:“编译错误:参数数量错误或属性分配无效”
【发布时间】:2022-07-27 23:51:06
【问题描述】:

运行以下代码时出现编译错误。代码的目标是移动光标并单击 excel 文档中的单元格。 mouse_event 似乎是导致问题的原因,但我不确定为什么(可能是格式错误?)。

VBA 编辑器还将前两行突出显示为红色,我不知道为什么会这样。

感谢您的帮助!

Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr) 

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As LongPtr = &H8
Public Const MOUSEEVENTF_RIGHTUP As LongPtr = &H10

Sub MoveMousePlease()

Dim i As Integer

For i = 1 To 9999
    'For Info, number of iteration
    'Cells(1, 1) = i
    
    If Cells(3, 5) = "" Then
        SetCursorPos 350, 300 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        
        SetCursorPos 350, 360 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        SetCursorPos 350, 420 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        SetCursorPos 350, 480 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
    Else
        Exit For
    End If
    Next i
End Sub


Sub WaitPlease()
    Dim sngWaitEnd As Single
    sngWaitEnd = Timer + 5
    Do
      DoEvents
      Cells(3, 3).Value = Timer
    Loop Until Timer >= sngWaitEnd
End Sub

【问题讨论】:

  • 32 位还是 64 位 Excel?

标签: excel vba compilation


【解决方案1】:

首先,不要那样做。您可以使用 VBA 命令完成所有操作,而无需移动鼠标和模拟点击。

如果你read the documentation 你会发现他们说不要使用mouse_event 来支持SendInput

现在答案:

您的代码在 64 位 Excel 中工作。这意味着您使用的是 32 位主机,需要不同的签名。

试试这个:

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Boolean
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多