I am working on a VBA macro which I would like to add buttons to each row. I would like to assign the same macro to each button, with different arguments on each row. The macro the button runs is a simple one which generates an email from information on the row the button is on and saves it to the user's outlook drafts folder.
The issue I am having is that when I have the email code classified as a function, it immediately runs through instead of assigning it to the button. When I have it classified as a sub, I receive a compile error which states 'Compile error: Expected Function or variable'
The code for the main macro follows:
Sub addButtons()
Dim lastCol As Integer
Dim lastRow As Integer
Dim r As Range
Dim btn As Button
Dim uid As String
Dim rDate As String
Dim i As Integer
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
rDate = Str(Date)
rDate = Replace(rDate, "/", "D")
For i = 2 To lastRow
Set r = ActiveSheet.Range(Cells(i, lastCol), Cells(i, lastCol))
Set btn = ActiveSheet.Buttons.Add(r.Left, r.Top, r.Width, r.Height)
uid = ActiveSheet.Cells(i, 1).Text
With btn
.OnAction = newhireEmail(i, rDate)
.Caption = "Email " & uid & "?"
.name = "btn" & (i - 1)
End With
Next i
r.EntireColumn.ColumnWidth = 15
End Sub
I can post the code for the email as well, but I don't believe it should be relevant as I would ideally like that code to not be run at all unless the button is being clicked.
Thank you for your time!