Excel VBA FormulaR1C1 Error 1004

Squareroot

New Member
Joined
Nov 9, 2022
Messages
15
Office Version
  1. 2021
Platform
  1. Windows
Hi,
I am getting 1004 error for the following code, i have checked through many time and still unsure why..

ActiveCell.FormulaR1C1 = "=IF(" & buffer2(0) & "=""yes"",IF(" & buffer2(1) & "=0," _
& buffer2(2) & "*0.07,SUMPRODUCT(" & buffer2(1) & "," & buffer2(3) & ") * 0.07),0)"
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Are the buffer values text? Then you're missing quotes, I think.
This works for me if I include the range (often 1004 errors are range related):
activesheet.range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This also works:
range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This doesn't raise an error but does produce a weird formula result:
activecell.FormulaR1C1 = "=IF(I6=""Blind""," & """yes"",""no"")"
 
Upvote 0
Are the buffer values text? Then you're missing quotes, I think.
This works for me if I include the range (often 1004 errors are range related):
activesheet.range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This also works:
range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This doesn't raise an error but does produce a weird formula result:
activecell.FormulaR1C1 = "=IF(I6=""Blind""," & """yes"",""no"")"
hi Miron,
Thanks for the reply.
The buffer is a array i use to store cells.
This is my code to store the cell into buffer.

For x = 0 To 3
header = Array("Include GST", "Cost/Quotation", "Online Price", "Qty")

Sheets(1).Select
Sheets(1).Cells.Find(What:=header(x), After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.Offset(1, 0).Select

Set buffer2(x) = Selection
buffer2(x) = ActiveCell
Debug.Print buffer2(x).Address
Next x
the above code should be correct cause when i print the address it give me the result i want

but after that i try to set one cell to the following formula it gives me error
following is the result i trying to achieve
=IF(H2="yes",IF(I2=0,F2*0.07,SUMPRODUCT(I2,J2) * 0.07),0)

I try to use the variable because i don't want to fix the location, as the location of the header might change
 
Upvote 0
Are the buffer values text? Then you're missing quotes, I think.
This works for me if I include the range (often 1004 errors are range related):
activesheet.range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This also works:
range("K6").formula = "=IF(I6=""Blind""," & """yes"",""no"")"

This doesn't raise an error but does produce a weird formula result:
activecell.FormulaR1C1 = "=IF(I6=""Blind""," & """yes"",""no"")"
to add on:
The value in the cells i try to store to buffer can be text or number.
 
Upvote 0
The value in the cells i try to store to buffer can be text or number.
I see that as a problem. If they are text they need to be delimited ("text"). If they are numbers then no.
The work around might be to treat all as text. Not sure if just adding delimiters would be enough, or if you'd have to convert as well (Cstr function). Probably not. I'd start with fixing the missing delimiters and see if that does it. I don't know where to put them or how many together. Maybe like

"=IF(""" & buffer2(0) & """=""yes"",IF(""" & buffer2(1) & """=0,""" _
& buffer2(2) & """*0.07,SUMPRODUCT(""" & buffer2(1) & """," & buffer2(3) & """) * 0.07),0)"
Don't know if you get my drift but I'm saying when you concatenate the buffer value with your original expression, text buffer values have to be delimited the same as where you have ""yes"". Likely what I did there is not the solution as 3 in a row might escapes to happen (coding term) leaving you with the wrong count. May have to be 4 """" not 3. Or like "=IF(" & """ & buffer2(0) & """=""yes"" or even 4's , not 3's.

Something weird happened there when I submitted. I think it's all there though. 1 am here and I'm not seeing straight anymore so that's it.
 
Upvote 0
You say that buffer2 is an array of cells.
If you want the cell values inserted into the formula, your code should work, but I'd like to see what
debut.print "=IF(" & buffer2(0) & "=""yes"",IF(" & buffer2(1) & "=0," & buffer2(2) & "*0.07,SUMPRODUCT(" & buffer2(1) & "," & buffer2(3) & ") * 0.07),0)"

returns.

If you want the cell addresses inserted into the formula, use buffer2(1).Address(,,xlR1C1)
 
Upvote 0
Solution
You say that buffer2 is an array of cells.
If you want the cell values inserted into the formula, your code should work, but I'd like to see what
debut.print "=IF(" & buffer2(0) & "=""yes"",IF(" & buffer2(1) & "=0," & buffer2(2) & "*0.07,SUMPRODUCT(" & buffer2(1) & "," & buffer2(3) & ") * 0.07),0)"

returns.

If you want the cell addresses inserted into the formula, use buffer2(1).Address(,,xlR1C1)
yes , I want to insert the address thankyou so much!!
 
Upvote 0
Hmm, value doesn't imply cell address to me. Hope you have a solution now anyway.
PS maybe i am not clear with my questions as i am new to VBA. what i want is just to insert the address of a cell in a formula but the address can change because i am not sure if all the headers inside the PDF i load inside excel will have the same arrangement. so i am trying to store the cell into a buffer and access the address from there. Anyways thanks for the response.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

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