Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FormulaArray error when entered from VBA

Posted on 2012-03-27
2
Medium Priority
?
357 Views
Last Modified: 2012-06-27
I used the code below to add a Formula Array. I get Error 1004 "Unable to set the FormulaArray propery of the Range class".
http://support.microsoft.com/kb/213181 indicates that this error occurs if the length of the formula exceeds 255 characters. The formula I am trying to enter is length 217.
So, what is the problem here?
Regards
Brian

ub UniqueList()
    Const Title = "SET UP UNIQUE LIST using an array formula"
    With Application
        CalculationSave = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
            SelectionSave = Selection.Address
            Set where = .InputBox("Select the Range using the mouse or accept this range:", Title, Selection.Address, Type:=8)
            If where.Parent.Parent.FullName <> ThisWorkbook.FullName Then GoTo Goodbye
            biglist = "l.i" & (CountNames("l.i") + 1)
            ThisWorkbook.Names.Add Name:=biglist, RefersTo:="='" & where.Parent.Name & "'!" & where.Resize(where.Rows.Count + 1, 1).Address
            Set anchor = .InputBox("Select the Cell  B E L O W  which to place the unique list", Title, Selection.Address, Type:=8)
            If anchor.Parent.Parent.FullName <> ThisWorkbook.FullName Then GoTo Goodbye
            uniqueresult = "a.o" & (CountNames("a.o") + 1)
            ThisWorkbook.Names.Add Name:=uniqueresult, RefersTo:="='" & anchor.Parent.Name & "'!" & anchor.Address
            anchor.Offset(1, 0).Resize(where.Rows.Count, 1).FormulaArray = BigFormula(biglist, uniqueresult)
Goodbye:
        .Calculation = CalculationSave
        .EnableEvents = True
    End With
End Sub
Function BigFormula(listi, anch) As String
    BigFormula = "=IF((ROW()-ROW(" & anch _
     & "))>=SUMPRODUCT(1/COUNTIF(" & listi _
     & "," & listi _
     & "&"""""")),"""""",""""""&INDEX(" & listi _
     & ",SMALL(IF(" & listi _
     & "="""""",ROWS(" & listi _
     & ")-1,IF(MATCH(" & listi _
     & "," & listi _
     & ",0)=ROW(" & listi _
     & ")-CELL(""" & Row & """," & listi _
     & ")+1,ROW(" & listi _
     & ")-CELL(""" & Row & """," & listi _
     & ")+1,ROWS(" & listi _
     & ")-1)),ROW()-ROW(" & anch _
     & "))))"
End Function
Function CountNames(Which)
    Dim myNames As Name
        For Each myname In ThisWorkbook.Names
           If Left(myname.Name, 3) = Which Then CountNames = CountNames + 1
        Next myname
End Function

Open in new window

0
Comment
Question by:canesbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 37775084
I haven't gone into the depths of your formula here, but you seem to have too many double quotes in your big formula - you have 6 where you want to insert a null string instead of 4, so you get 3 double quotes in the output. Try:
Function BigFormula(listi, anch) As String
    BigFormula = "=IF((ROW()-ROW(" & anch _
     & "))>=SUMPRODUCT(1/COUNTIF(" & listi _
     & "," & listi _
     & "&"""")),"""",""""&INDEX(" & listi _
     & ",SMALL(IF(" & listi _
     & "="""",ROWS(" & listi _
     & ")-1,IF(MATCH(" & listi _
     & "," & listi _
     & ",0)=ROW(" & listi _
     & ")-CELL(""" & Row & """," & listi _
     & ")+1,ROW(" & listi _
     & ")-CELL(""" & Row & """," & listi _
     & ")+1,ROWS(" & listi _
     & ")-1)),ROW()-ROW(" & anch _
     & "))))"
End Function

Open in new window

I debugged this by breaking on the the failing line, typing:
?BigFormula(biglist, uniqueresult)

Open in new window

in the immediate window, the pasting this into Excel - this gives you a bit of help with what's going wrong!
0
 

Author Comment

by:canesbr
ID: 37776610
@AndrewSSD3
Thanks - that's it.
Note:  I need  & ")-CELL(""" & "Row" & """," & listi _ in lines 11 and 13.
Note: Once again the error messages and MS articles proved to be of scant help.
Regards
Brian
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question