Runtime Error 1004 Copy long array formula in vba - excel

I have the following array formula in cell B2 in my Excel spreadsheet:
={IF(COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))>=COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)));IF(COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))<=COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))))}
Now I want to use the following VBA code to copy this code into cell A2:
Sheets("Sheet1").Range("A2").FormulaArray = Sheets("Sheet1").Range("B2").Formula
However, when I use this code I get runtime error 1004.
Do you have any idea how to solve this issue?

character limit of 255 when passing arrays...
https://support.microsoft.com/en-us/kb/213181
Set up named ranges for a30:A1000 of A, B30:B1000 of B, and c30:c1000 of D (C is reserved so you can't use it and CC will make this formula too long also) (ctrl+f3 to open the named range manager and then hit new button)
Then Change your formula to use the named ranges.
=IF(COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))>=COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(D>A-1,A))),IF(COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))<=COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))))
Alternately you could set up custom VBA functions that would allow you to replace the repetitive code within your if statement with shorter strings of characters to bring you under the 255. But ultimately you need to get under 255. Once under 255 your code works fine, either with my changes above or with a shorter array formula. (note that I changed the semi-colons to commas so you might need to change them back if you use semi-colons)

I believe the issue lies with the references within the formula. As the cell B2 references A30:A1000 in the formula, when you apply this to the cell A2 it tries to move the reference one column to the left - which it can't. In order to bypass this you can make the references absolute:
={IF(COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))>=COUNT(IF(ISNUMBER($A$30:$A$1000);IF($B$30:$B$1000>$A$30:$A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)));IF(COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))<=COUNT(IF(ISNUMBER($A$30:$A1000);IF(B30:B1000>A30:A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($B$30:$B$1000>$A$30:$A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))))}

Related

Concatenating Inside an Excel Shape

Is it possible to use a Concatenate formula (or using &) inside an Excel shape?
So I understand you can link a cell inside an Excel shape (e.g. =A1) but is it possible to write ="Total YTD "&A1 (if A1 is a total #) or =CONCATENATE("Total YTD ",A1)
When I try to do this, it gives me the error "This formula is missing a range reference or a defined name"
Have you tried doing the formula work in one cell and then referencing the cell?
It is indirect, but it overcomes the apparent limitation of the shape object. It will also let you use special characters like &Char(13) to generate carriage returns in the text box as well.
An alternate means may be to copy a cell containing the formula and pasting it as a linked picture. With this it will allow you to effect the object with conditional formatting.

Using excel vba, how to write a formula in a cell

In Excel, A1 is equal to =B1/ BDP(C11&”Corp”, “ds036”)
Which BDP(Corp, ds036) is a function and parameters from Bloomberg.
How to use Excel VBA for this formula?
I was trying different ways in VBA to solve my point. One of them is like the line below,
Cells(1,1)=“B1/ BDP(C11&”Corp”, “ds036”)”
An other way I tried, to simplify,
For i=1 to10
Cells(i,1)=“Cells(i,2)/ BDP(cells(i,3)&”Corp”, “ds036”)”
Next
Also, if it can access directly to BDP function. That will be perfect!
try:
Cells(1,1).Formula = "=B1/ BDP(C11&""Corp"", ""ds036"")"
Note:
I used a different flavor of double quote
I doubled-up on the double quotes
Does this do what you want?
Range("A1:A10").Formula = "=B1/BDP(C3&""Corp"",""ds036"")"
If you assign a formula to a multi-cell range, I think Excel will automatically adjust relative cell references for subsequent rows/columns - similar to CTRL+D or dragging down.
This means you don't need to loop through each row and re-construct the formula string for each loop iteration.

Excel change strings in formulas while copying

I have a cell number as a string ("A1") in a formula, and this needs to be changed relatively while copying, like it does automatically to normal cell references. so I don't have to visit 400 cells and change them all by hand.
Any way I can manage this? Thanks for the help.
Formula I am using in 400 cells:
=IF(IDENTIC($M$10;"Entries are correct.");IF(ISNUMBER(FIND("A1";$N$5));"S1";IF(ISNUMBER(FIND("A1";$N$6));"S2";IF(ISNUMBER(FIND("A1";$N$7));"S3";IF(ISNUMBER(FIND("A";$N$8));"S4";IF(ISNUMBER(FINDEN("A1";$N$9));"S5";"")))));"")
I don't want to change every single cell as text in all 400 cells, it should change relatively as normal cell references do.
To get a cell reference as a string in Excel you can use the CELL function:
=CELL("address";A1)
Since the second parameter to the function is an actual cell reference, if you copy and paste this formula it will auto-update accordingly.
Bear in mind this will return a string containing an absolute address so it will return "$A$1", not "A1".
CELL(...) will work.
More generally, you can also build the formula as strings using concatenations. This can be useful when you have a list of stuff and you would like to have formulas that refer to those names. Or in your case it would also work.
Building the formula as string with concats & then search & replace
First I build up as much as I can the formula so that the general structure is already formed (only the reference you'll want to change will not be final):
Using your case as an example:
Then, I wrap that nearly-formed formula into a CONCATENATE(). I then arrange things to have a references that I will point to within that CONCAT() that will auto-increment to the text I actually want to include in my formula. Something like this:
=CONCATENATE("IF(FIND(""",A1,"""$A$4),TRUE, FALSE)")
The ,A1, refers to the actual cell, so that cell's content is what will be included in my formula. You can then increment this all the way to build the proper formula for each cell (the 400 ones in your case). You need the triple """ because you want to actually include A1, B1 etc... are text references, not cells.
Then, all you need to do is selected all those formulas, copy them & make sure to choose past as values, say the line under (to keep you concatenate intact so you can edit it later if needed). Then with that pasted line all selected, you use a find & replace to add an "=" sign to the beginning of formulas. Better be too specific on the find & replace, so find "IF(F" and replace by "=IF(F" or similar. Then magic happens:
You can see that A1, C1, D1 have returned TRUE while the rest are #VALUE! because the find method has failed to find the string.

Excel - How to use the value of a cell as the row value of another cell?

Here is my problem:
I have a cell (V4) containing the value 444. I want to use this value in the formula of another cell (M12) in the following way. I want the formula to be equivalent to =MIN(L12:L444) but instead of 444 I want to refer to cell V4 which contains the value 444. But when I type in =MIN(L12:L(V4)) it obviously doesnt work so how do I do it? Sorry if I didn't explain it very well. :S
Would this work for you:
=MIN(L12:INDIRECT("L"&$V$4))
From: Excel - INDIRECT and Using the value in a cell as a cell reference in a formula?
INDIRECT will work and is closest to the solution you described, but I prefer OFFSET, which uses proper references. (For example, if you insert a column in the sheet before L, INDIRECT will break while OFFSET will just update its reference as expected.
Two ways to go with OFFSET:
1 - Start at L$1 and go down $V$4-1 rows. (This will work with $V$4 as you've defined it now.)
=MIN(L12:OFFSET(L$1,$V$4-1,0))
2 - In $V$4, provide the height of the range you want.
=MIN(OFFSET(L12,0,0,$V$4,0))
It's hard to make suggestions without more context, but I'm sure you can tweak one of these patterns to meet your needs.
I am not sure if you are trying to include all of the values in 1 column and then on non-contiguous cell. If so, it should look like = Min(L12:L444,V4) . The L12:L444 looks at the value in every cell in the L column from 12 - 444.
So you can check individual cell (A3, D15, Q54) with commas, or a range of cells (A3:Z54) with a colon. Or a range and a specific cell like above =Min(L12:L444, V4).

Excel Custom Data Validation

I have to restrict some invalid data in a Excel Column.
Below is Validation Criteria:
Should be a numeric Number
Size/Length should be equals to 9
Ex : valid:602005514, invalid:were,43456 etc.
I have created a Custom Data Validation using below function.
=AND(LEN(INDIRECT(SUBSTITUTE(ADDRESS(ROW(),COLUMN()),"$","")))=9,
ISNUMBER(INDIRECT(SUBSTITUTE(ADDRESS(ROW(),COLUMN()),"$",""))))
//here i have to get cell name dynamically. so, i have used ADDRESS function
But its not working as expected. It is not allowing any value in cell.
I guess it might be because of LEN function circular reference.
I have tried many ways, but not able to solve it. Please help me to solve this.
You don't need to resort to INDIRECT etc, in you formula just refer to top left cell in the range you are applying the validation.to. Excel will adjust for the othger cells in the range. When entering cell references don't use absolute references, ie no $'s
Eg select a range starting at cell A1, and set data validation formula to
=AND(ISNUMBER(A1),LEN(A1)=9)
Check this simple validation .. checking Cell D13 ..
=IF(LEN(D13)=9,IF(ISNUMBER(D13),"yes","no"),"no")
Another way:
=AND(ISNUMBER(A1),A1>99999999)
Make sure there are no leading or trailing spaces in the cell:
=IF(TRIM(A3)=A3,TRUE,FALSE)

Resources