Solved

FormulaArray error when entered from VBA

Posted on 2012-03-27
2
350 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

695 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