+ Reply to Thread
Results 1 to 14 of 14

Error assigning formula to .FormulaArray

  1. #1
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Error assigning formula to .FormulaArray

    Hi,

    I am looking for some help with a macro to paste an array formula into a range of cells. I know in principle the VBA is written OK as i use it in other spreadsheets. The main difference here is this one is an Array formula. When i run the code i get the error "Unable to set the FormulaArray property of the Range class".

    Any ideas why i get that error? It looks OK to me. Here is the code:

    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 06-15-2021 at 03:59 PM. Reason: more specific title

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Error assigning formula to .FormulaArray

    The first problem I noticed right away was that you are using quotes within a quoted string. I am surprised this compiles, but it does. I don't know how it is parsed, but you need to double the quotes:

    Please Login or Register  to view this content.

    Also I didn't think the syntax looked correct so I broke it down

    Please Login or Register  to view this content.
    Things go off the rails in your second MATCH formula. Immediately after the Range specification ($R$10:$DE$10) you have a concatenation to to a long LOOKUP expression which will not give you a valid Range for the MATCH.

    Without a lot more information I can't tell what is your objective so I can't take what you have and correct it.

    Also, just my recommendation, if you are going to use the comma for the last argument in MATCH, go ahead and explicitly include the default argument of 1. It is not an error to omit it, but it provides built-in documentation for what you are doing and demonstrates intent.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Error assigning formula to .FormulaArray

    Your formula is far too long for the FormulaArray property which is limited to 255 characters (yours is over 700).

    Also, the way your code is written, you will get the same values in every row.
    Rory

  4. #4
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    Hi,

    Thanks both for the reply. The formula works just fine when its a formula in the spreadsheet and it returns the correct values for each row applied, so I am not sure I understand your comment on it not working.

    I have attached a sample file to demonstrate what I am doing and that the formula works directly in the file. Lets assume Sheet 2 is blank, I wanted a VBA to paste the formula in range D2:P30 to return the values displayed. (I have only copied across so far but you get the idea)

    Many thanks for any help.
    Attached Files Attached Files

  5. #5
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Error assigning formula to .FormulaArray

    1. In VBA the Formulaarray property only accepts formulas up to 255 characters, as I said. The only workarounds are using a non-array formula if possible, or entering a shorter formula as an array, then replacing parts of it with the actual calculations you want using the Range.Replace method.

    2. If you write Range("D2:O200").FormulaArray = then you will get exactly the same formula in every cell. In other words the $B2 reference will not adjust at all. It's the equivalent of you selecting that entire range, typing the formula and then pressing Ctrl+Shift+Enter. It is not the same as you array entering that into one cell and then filling across/down.

  6. #6
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    OK, thank you for the explanation, i now understand your original point.

    Are you able to suggest a work around as i am unsure how to shorten the formula or use range.replace?

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Error assigning formula to .FormulaArray

    You could try something like this (untested):

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    Unfortunately the 2nd .Replace line returns a Syntax error?

    Edit: corrected this by removing the brackets, but it still does not like that line.
    Last edited by Kcroft9; 06-16-2021 at 08:13 AM.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Error assigning formula to .FormulaArray

    Looks like you need to remove the first parenthesis

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    The amendment gets past the Syntax error but still debugs due to that line but it doesn't give any suggestion as to why

    Edit: the error is "type mismatch"
    Last edited by Kcroft9; 06-16-2021 at 08:20 AM.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Error assigning formula to .FormulaArray

    Sorry, I didn't spot the quotes in the formula. Second one should be:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    Each amendment doesn't seem to work. As i always get run time error 13 on that line. I guess it doesn't help that the formula you are helping with references a network location so you are unable to test it.

    F9 is Sheet 2 has the same formula we are using but references a location directly in that test file. Is it worth using that in the VBA code to test it works and then i can modify to the network location?

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Error assigning formula to .FormulaArray

    This code runs for me in your sample workbook:

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    02-28-2019
    Location
    Peterborough
    MS-Off Ver
    2013
    Posts
    94

    Re: Error assigning formula to .FormulaArray

    You are right, that code works perfectly fine in the sample file. There must be an issue with my destination location so I will look into that.

    Thanks a lot for you help. And the education lesson regarding array formulas in VBA

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. .FormulaArray causes Run Time Error 1004, Unable to set the FormulaArray property
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2017, 03:12 PM
  2. [SOLVED] .FormulaArray =
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-20-2016, 09:49 AM
  3. [SOLVED] FormulaArray is non-functional in VBA
    By HJHamm in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-14-2015, 01:04 PM
  4. VBA FormulaArray
    By jadown in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 11:52 AM
  5. [SOLVED] FormulaArray
    By GreyBates in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2013, 04:56 PM
  6. FormulaArray
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-29-2011, 06:32 PM
  7. Using FormulaArray in VBA
    By marcelobf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2006, 04:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1