I've search up and down and cannot seem to find the answer to my question.
I'm trying to run a macro every 30 minutes. The workbook can be kept open but doesn't need to be. I DO have 3 other excel workbooks that MUST be kept open the whole day as this is for a hedge fund and they provide real time data using the bloomberg API. The macro I'm trying to run sends an email with a PnL update of our portfolios throughout the day. Running the macro on it's own works great. The macro is here:
Public Sub PnLUpdate()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range, cell As Range, HtmlContent As String, i As Long, j As Long
Set rng = Range("A1:M300")
HtmlContent = "<table>"
For i = 1 To rng.Rows.Count + 1
HtmlContent = HtmlContent & "<tr>"
For j = 1 To rng.Columns.Count + 1
HtmlContent = HtmlContent & "<td>" & Cells(i, j).Value & "</td>"
Next
HtmlContent = HtmlContent & "</tr>"
Next
HtmlContent = HtmlContent & "</table>"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "xx"
.CC = "xx"
.Subject = "PnL Update // " & Format(Now, "mm-dd-yy // hh:mm AM/PM")
.HTMLBody = HtmlContent
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End Sub
I've tried inserting
Application.OnTime Now + TimeValue("00:00:30"), "PnLUpdate"
Into said macro. It works and sends the email every 30 minutes. Issue is that it sends whatever the active sheet is at the time, not the sheet I need it to send.
I tried writing a module that looks like this:
Sub callPnLUpdate()
With Sheet10
Call .PnLUpdate
Application.OnTime Now + TimeValue("00:00:30"), "PnLUpdate"
End With
End Sub
This gives me the error "Cannot run the macro 'worksheetname.Xlsx!PnLUpdate'. The macro may not be available in this workbook or all macros may be disabled.
I tried searching for the solution to this issue but all the solutions say to enable programmatic access which I've already done and that doesn't work either.
To be clear: the solution to this problem does NOT have to be to use my macro. I will accept any solution which can send this specific spreadsheet every 30 minutes (preferably starting at 9:31am and ending at 4:05pm but that's less important than getting this automation to work every 30 minutes).
Any help is very much appreciated.
Thank you!