How can I define a sub or function in VBA?
This is my code:
Private Sub CommandButton1_Click()
Call Send_Mail
End Sub
In Worksheet "Sheet1" I have a CommandButton called Send_Mail and in "Sheet2" I have also a CommandButton. When I click the CommandButton in Sheet2 I want that the Button in Sheet1 will run.
With my code the : error "Sub or Function is not defined" appears.
EDIT:
Code for 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
Sub Send_Mailis located in a regular code module, and not a worksheet module, that way it is accessibleWhen I click the CommandButton in Sheet2 I want that the Button in Sheet1 will run, you mean the function assigned to button 1?