Solved

FormulaArray error when entered from VBA

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now