Excel VBA-定义子或功能



如何在VBA中定义sub或函数?

这是我的代码:

Private Sub CommandButton1_Click()
    Call Send_Mail
End Sub

在工作表" Sheet1"中,我有一个CommandButton,称为 send_mail 和" Sheet2"中,我也有CommandButton。当我单击Sheep2中的CommandButton时,我希望Sheet1中的按钮将运行。

使用我的代码:错误"未定义sub或函数"。

编辑:

send_mail的代码:

Public Sub Send_Mail_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim nameList As String
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error GoTo cleanup
    For i = 4 To 22                                                                         
        If Range("B4").Value <> "" Then                                                        
            nameList = nameList & ";" & Range("C" & i).Value
        End If
    Next
        With OutMail
            .To = nameList
            .Subject = "Subject Line"
            .Body = "Body Text"
            .Send
        End With
cleanup:
    Set OutApp = Nothing
    MsgBox "E-Mail sent."
    MsgBox Err.Description
End Sub

我不知道Sheet1按钮附加了什么基本子过程,但它可能具有相似的名称。

'Sheet2's button sub procedure
Private Sub CommandButton1_Click()
    Call Sheet1.CommandButton1_Click
End Sub

最新更新