Help with inserting Excel formula into VBA code

numberchomper

New Member
Joined
Aug 19, 2009
Messages
13
I found the following code on this site that does exactly what I want. It counts the number of rows in <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:State w:st="on"><st1:place w:st="on">Col</st1:place></st1:State> A and then copies the value of the formula in Col G starting at G7 and continuing to the last row. Perfect!
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Sheets("0015 Balance Sheet").Select<o:p></o:p>
Range("A1").Select<o:p></o:p>
Dim LR As Long<o:p></o:p>
LR = Range("A" & Rows.Count).End(xlUp).Row<o:p></o:p>
With Range("G7:G" & LR)<o:p></o:p>
.FormulaR1C1 = "=SUMIF('0015 Trial Balance'!C1:C7,'0015 Balance Sheet'!RC[-6],'0015 Trial Balance'!C2)"<o:p></o:p>
.Value = .Value<o:p></o:p>
End With
<o:p></o:p>
However, now I wish to replace the existing formula with the following Excel formula:
<o:p></o:p>
=IF((SUMIF(‘0015 Trial Balance’!$A:$A,’0015 Income Statement’!A7,’0015 Trial Balance’!$D:$D)-‘0015 Income Statement’!B7)=0,”OK”, IF(SUMIF(‘0015 Trial Balance’!$A:$A, “Net Income Before Change”,’0015 Trial Balance’!$D:$D)-b7=0,”OK”,”????”))
<o:p></o:p>
Excel will not let me record a macro using this formula and when I input directly into the code, I get a “Syntax Error”. I even tried converting it to R1C1 format and received the same error.
<o:p></o:p>
Can someone please help me input this formula into code? I really appreciate any and all feedback.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this
Code:
=IF((SUMIF('0015 Trial Balance'!$A:$A,'0015 Income Statement'!A7,'0015 Trial Balance'!$D:$D)-'0015 Income Statement'!B7)=0,"OK", IF(SUMIF('0015 Trial Balance'!$A:$A, "Net Income Before Change",'0015 Trial Balance'!$D:$D)-B7=0,"OK","????"))

Your single and double quotes were the wrong characters... ie

" does not equal
 
Upvote 0
Thanks for your response!

I made the change to the single and double quotes. Now I get a both a syntax error and a "end of statement expected" message at the first "OK" in the formula.

Any other suggestions?
 
Upvote 0
Sounds like you still have a bad double or single quote in there... you should be able to just cut and paste the following...

=IF((SUMIF('0015 Trial Balance'!$A:$A,'0015 Income Statement'!A7,'0015 Trial Balance'!$D:$D)-'0015 Income Statement'!B7)=0,"OK", IF(SUMIF('0015 Trial Balance'!$A:$A, "Net Income Before Change",'0015 Trial Balance'!$D:$D)-B7=0,"OK","????"))
 
Upvote 0
Thanks again for the response!

I have cut and pasted your formula into the code. It now appears as follows:

Sheets("0015 Balance Sheet").Select
Range("A1").Select
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("G7:G" & LR)
.FormulaR1C1 = "=IF((SUMIF('0015 Trial Balance'!$A:$A,'0015 Income Statement'!A7,'0015 Trial Balance'!$D:$D)-'0015 Income Statement'!B7)=0,"OK", IF(SUMIF('0015 Trial Balance'!$A:$A, "Net Income Before Change",'0015 Trial Balance'!$D:$D)-B7=0,"OK","????"))"
.Value = .Value
End With

I am still getting a compile error and the first "OK" is highlighted. The error I am getting is:

Compile error:
Expected: end of statement

I am sorry I am having so much trouble with this. It seems that it should be fairly straight-forward. However, I do appreciate your efforts very much!
 
Upvote 0
Code:
=IF(SUMIF('0015 Trial Balance'!$A:$A,'0015 Income Statement'!A7,'0015 Trial Balance'!$D:$D-'0015 Income Statement'!B7)=0,"OK", IF(SUMIF('0015 Trial Balance'!$A:$A, "Net Income Before Change",'0015 Trial Balance'!$D:$D)-B7=0,"OK","????"))

=IF(SUMIF('0015 Trial Balance'!$A:$A,'0015 Income Statement'!A7,'0015 Trial Balance'!$D:$D-'0015 Income Statement'!B7)=0,"OK", IF(SUMIF('0015 Trial Balance'!$A:$A, "Net Income Before Change",'0015 Trial Balance'!$D:$D)-B7=0,"OK","????"))
 
Upvote 0
Thanks again for the response!

I was able to find the cause of the problem in another post. It appears you have to have double quotes ("") around text items, i.e. "OK", in the formulas.

Also, I had to convert the formula to R1C1 format. That was a chore!:(

But everything works fine now! I would have never thought quotes could cause so much of a hassle!

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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