Excel VBA - Runtime Error 1004

bryan___

New Member
Joined
Aug 4, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I need to write this array formula into a cell, but every time I run my macro, I get a runtime error 1004. From my understanding, this is because my array formula is over 255 characters. I've tried multiple methods of replacing parts of the formula with temporary strings (to make the formula <255 characters) and replacing them afterwards, but I still get the same runtime error. I was wondering if anyone could help. Here is the code:

Range("B1").Value = 0
'Selection.FormulaArray = _
"=IFERROR(IF(RC[-1]=1," & Chr(10) & """""&INDEX(R1C2:R[-1]C,MAX(--(R1C1:R[-1]C[-1]=RC[-1])*(ROW(R1C1:R[-1]C[-1]))))+1," & Chr(10) & "IF(RC[-1]>R[-1]C[-1]," & "R[-1]C&"".1""," & Chr(10) & "IF(RC[-1]=R[-1]C[-1]," & Chr(10) & "LEFT(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",LEN(R[-1]C)-LEN(SUBSTITUTE(R[-1]C,""."","""")))))&RIGHT(R[-1]C,LEN(R[-1]C)-FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",LEN(R[-1]C)-LEN(SUBSTITUTE(R[-1]C,""" & "."","""")))))+1," & Chr(10) & "LEFT(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1)))&MID(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1))+1,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]))-FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1)))+1))),""""&0)" & ""
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
More liklely the '&Chr(10)&' line feeds. If you use line breaks in Excel formulas it needs to be '&Char(10)&'
 
Last edited:
Upvote 0
I ran out of edit time on previous post.
I am not sure what you are trying to do with the line feed, but in Excel formulas the Char(10) is used to place cell values beneath each other in the return cell. Example:
Wrap text must be on
Formula in cell C3:
Rich (BB code):
=A2&Char(10)&B2&Char(10)&C2
 
Upvote 0
I ran out of edit time on previous post.
I am not sure what you are trying to do with the line feed, but in Excel formulas the Char(10) is used to place cell values beneath each other in the return cell. Example:
Wrap text must be on
Formula in cell C3:
Rich (BB code):
=A2&Char(10)&B2&Char(10)&C2
The line feeds are not necessary. I used the "record macro" feature to put in the formula, so I didn't have to type it into R1C1 style by hand. I got rid of those parts to reduce complications. After getting rid of the Chr(10) parts, I still get the same runtime error. Here is what I am trying to do:

Range("B1").Value = 0
Range("B2").Select

Selection.FormulaArray = "=IFERROR(IF(RC[-1]=1,""""&INDEX(R1C2:R[-1]C,s_one,IF(RC[-1]>R[-1]C[-1],R[-1]C&"".1"",IF(RC[-1]=R[-1]C[-1],s_two,s_three))),""""&0)"

Selection.Replace "s_one", "MAX(--(R1C1:R[-1]C[-1]=RC[-1])*(ROW(R1C1:R[-1]C[-1]))))+1"

Selection.Replace "s_two", "LEFT(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",LEN(R[-1]C)-LEN(SUBSTITUTE(R[-1]C,""."","""")))))&RIGHT(R[-1]C,LEN(R[-1]C)-FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",LEN(R[-1]C)-LEN(SUBSTITUTE(R[-1]C,"".""," & """"")))))+1"

Selection.Replace "s_three", "LEFT(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1)))&MID(R[-1]C,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1))+1,FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]))-FIND(""|"",SUBSTITUTE(R[-1]C,""."",""|"",RC[-1]-1)))+1"
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

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