Formula works in Excel but not in VBA

Christip

New Member
Joined
Mar 8, 2018
Messages
7
The following formula works fine in vba:
faddress=Range(ActiveCell.Offset(0,-15),ActiveCell.Offset(0,-15).End(xlUp).End(xlToRight).Offset(0, -4)).Address
ActiveCell.FormulaArray = "=if(countif(" & faddress & ",""hrcmp"")>0,IF(COUNTIF(" & faddress & ",""hrpay"")>0,""HRCMP/HRPay Mix"",""""),"""")"

However, when I change the formula to this the 1004 error code occurs. I am sure it has something to do with mixing formula styles don't know how to fix it.

ActiveCell.Formula = "=IF(RC[-19]=R[-1]C[-19]),if(countif(" & faddress
&",""hrcmp"")>0,IF(COUNTIF(" & faddress & ",""hrpay"")>0,""HRCMP/HRPay Mix"","""")),""""))"

Please help... thank you in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hello, at first glance the things to adjust are

faddress=Range(ActiveCell.Offset(0,-15),ActiveCell.Offset(0,-15).End(xlUp).End(xlToRight).Offset(0, -4)).Address(ReferenceStyle:=xlR1C1)

ActiveCell.FormulaR1C1 = "=IF(RC[-19]=R[-1]C[-19]),if(countif(" & faddress
&",""hrcmp"")>0,IF(COUNTIF(" & faddress & ",""hrpay"")>0,""HRCMP/HRPay Mix"","""")),""""))"

also make sure that when you are returning the address to faddress you are setting the right values for ColumnAbsolute and RowAbsolute
 
Upvote 0
Hi VBA Geek,
Thanks for your response. I tried inputting that reference style change but I still receive the same error message. Any other suggestions?
 
Upvote 0
make sure the formula is not missing any parenthesis or commas, copy the formula in the immediate window and try to paste it manually on the worksheet
 
Upvote 0
I am sorry to be such a nag, but I copy pasted your formula from above just to be sure I didn't miss anything...I still get the Run Time Error 1004
 
Upvote 0
Before trying to put the formula in a cell, put it in a string variable and print it to the Immediate window. If it's correct, you should be able to copy it from there and paste it into the cell (after putting Excel in R1C1 mode). If not, you're hot on the trail.
 
Upvote 0
The problem is when I enter it manually in the worksheet it works fine. But when I try to run it with VBA that's when it fails the second version of the formula. All the string variables are verified.
 
Upvote 0
What do you get when you print it in the immediate window?

print this: "=IF(RC[-19]=R[-1]C[-19]),if(countif(" & faddress &",""hrcmp"")>0,IF(COUNTIF(" & faddress & ",""hrpay"")>0,""HRCMP/HRPay Mix"","""")),""""))"

also what is the activecell?
 
Upvote 0
My active cell is in column w, the row is dynamic.

When I print this in the immediate window = "=IF(RC[-19]=R[-1]C[-19]),if(countif(" & faddress &",""hrcmp"")>0,IF(COUNTIF(" & faddress & ",""hrpay"")>0,""HRCMP/HRPay Mix"","""")),""""))"

I get:
IF(RC[-19]=R[-1]C[-19]),if(countif(,"hrcmp")>0,IF(COUNTIF(,"hrpay")>0,"HRCMP/HRPay Mix","")),""))
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

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