excelvba中的委托数组
如何在Excel VBA中声明代理数组 我正在审核excel电子表格,每一列都需要一个不同的函数来确定单元格的好坏。每个函数的类型相同excelvba中的委托数组,vba,excel,Vba,Excel,如何在Excel VBA中声明代理数组 我正在审核excel电子表格,每一列都需要一个不同的函数来确定单元格的好坏。每个函数的类型相同 Private Function checkCell(auditCell As Range) As Boolean 如果我可以声明一个委托数组,我就可以将处理不同checkCell函数的顺序推送到一个函数,而不是实际审核工作表的函数,该函数可以成为一个嵌套循环,在我的委托数组中迭代 委托是否在Excel VBA中定义?VBA编辑器告诉我没有。AFAIK“开箱即
Private Function checkCell(auditCell As Range) As Boolean
如果我可以声明一个委托
数组,我就可以将处理不同checkCell
函数的顺序推送到一个函数,而不是实际审核工作表的函数,该函数可以成为一个嵌套循环,在我的委托数组中迭代
委托是否在Excel VBA中定义?VBA编辑器告诉我没有。AFAIK“开箱即用”不可用。话虽如此,您可以使用WIN32 API调用(从user32.dll
)实现委托
功能
下面是示例代码:
Option Explicit
'-----External Library Declaration which helps call the Proc by Address -----
Private Declare Function CallWindowProc _
Lib "user32.dll" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'-----This is the main function calling upon the proc via pointer -----
Public Sub test_delegate()
Dim sMessage As String
Dim nSubAddress 'As Long
'This message will be passed to our Sub as an argument
sMessage = InputBox("Please input a short message")
'Get the address to the sub we are going to call
nSubAddress = ProcPtr(AddressOf ShowMessage)
'Do the magic! Function Called via Pointer...
CallWindowProc nSubAddress, VarPtr(sMessage), 0&, 0&, 0&
End Sub
'-----This is the subroutine we want to call by address-----
Private Sub ShowMessage( _
msg As String, _
ByVal nUnused1 As Long, _
ByVal nUnused2 As Long, _
ByVal nUnused3 As Long)
'This is the Sub we will call by address
'it only use one argument but we need to pull the others
'from the stack, so they are just declared as Long values
MsgBox msg
End Sub
'-----This function is used to extract the address of value to long -----
Private Function ProcPtr(ByVal nAddress As Long) As Long
'Just return the address we just got
ProcPtr = nAddress
End Function
源代码:是否有其他user32.dll
函数可以复制此功能?你是说复制代理的数组
?只是代理
我希望我可以编写自己的代理
类并从那里继续。我刚刚将此代码从旧的32位将Excel安装到64位,使用CallWindowProc
和CopyMemory
ptrsafe,现在在nSubAddress=ProcPtr(ShowMessage的地址)
上出现类型不匹配错误。你知道这是否可以在x64 Excel上使用吗?