Run-time error 5

xenedra13

Board Regular
Joined
Aug 15, 2014
Messages
58
Every time I run this macro I get a run-time 5 error 'Invalid procedure call or argument' on each of the marked lines. What am I doing wrong???

Code:
Dim Cell As Range, Rng As Range
Dim Body As String, i As Integer
Dim JuneTemplate As String, JulyTemplate As String
Dim myOlApp As Object, MyItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set Rng = Range("A6:A57")
JuneTemplate = FILEPATH & FILENAME
JulyTemplate = FILEPATH & FILENAME
For i = 6 To 57
For Each Cell In Rng
    'If Checkboxes are checked...
    If Cells(i, 2) = True Then
        'Choosing June or July template...
        If Cells(i, 3).Value = "June" Then
            Set MyItem = myOlApp.CreateItemFromTemplate(JuneTemplate)
        ElseIf Cells(i, 3).Value = "July" Then
            Set MyItem = myOlApp.CreateItemFromTemplate(JulyTemplate)
        End If
        
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "CTD%1", FormatPercent(Abs(Cells(i, 7)) / (Cells(i, 8)) - 1))
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "CTD%2", FormatPercent(Abs(Cells(i, 8)) / (Cells(i, 9)) - 1))
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$R-PC1", FormatCurrency(Cells(i, 10)), 0)             'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$R-PC2", FormatCurrency(Cells(i, 11)), 0)             'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$CTD1", FormatCurrency(Cells(i, 12)), 0)              'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$CTD2", FormatCurrency(Cells(i, 13)), 0)              'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$NI1", FormatCurrency(Cells(i, 14)), 0)                'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$NI2", FormatCurrency(Cells(i, 15)), 0)                'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$CNI1", FormatCurrency(Cells(i, 16)), 0)              'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$CNI2", FormatCurrency(Cells(i, 17)), 0)              'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "PC%1", FormatPercent(Cells(i, 18)), 0)                'ERROR HERE
        MyItem.HTMLBody = Replace(MyItem.HTMLBody, "PC%2", FormatPercent(Cells(i, 19)), 0)                'ERROR HERE
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What's in the cell eg Cells(i, 10) when you get the error? I note that you have a For Each ... Next loop but you never use the Cell object within it.
 
Upvote 0
What's in the cell eg Cells(i, 10) when you get the error? I note that you have a For Each ... Next loop but you never use the Cell object within it.
It's a group of constantly changing dollar amounts. What do you mean by 'you never use the cell object within it'?
 
Upvote 0
What precisely is in the cell when you get the error?

You have:

Code:
For Each Cell In Rng

but you never reference Cell within the loop, at least not in the code you posted.
 
Upvote 0
Cells(I,10) contains
56,588

<tbody>
</tbody><colgroup><col></colgroup>

I wasn't really sure exactly what to put in the
Code:
For each Cell in Rng
part of the code. I'm kinda new to this. What should it be?
 
Upvote 0
Then it should be eg:

Code:
MyItem.HTMLBody = Replace(MyItem.HTMLBody, "$R-PC1", FormatCurrency(Cells(i, 10), 0))
 
Upvote 0
Thank you! That worked. Almost. I never would have caught that on my own. It completed I=6 to 10 but at #11 it stopped on this line
Code:
MyItem.HTMLBody = Replace(MyItem.HTMLBody, "CTD%1", FormatPercent(Abs(Cells(i, 7)) / (Cells(i, 8)) - 1))
and gave me a run-time error 6: Overflow. Any idea what is causing that?
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top