Reducing size of FormulaArray

rdilipk

New Member
Joined
Nov 17, 2011
Messages
36
I use the formula array property of a range to display 2D variant data into individual cells in Excel. So I have:

Sub AdjustRange(rows as Integer, cols as Integer)
Dim r as Range
set r = Range("A3").Resize(rows, cols)
r.FormulaArray = "=SomeWorkSheetFunctionThatReturnsAMatrix()"
End Sub

AdjustRange(4, 4) does what is expected.

With this in mind, assume you only know cell A3 but you have no idea if a formula array sits there or not. All you know is that when that VBA code is called again the range now needs to be modified to accept a bigger matrix. so if I do:

AdjustRange(6, 6)

This too works as expected.

However if the matrix size ends up shorter -- i.e lesser # of rows or columns, the call to FormulaArray fails with "Unable to set FormulaArray on the range".
Is this the right behavior? Any way to get around this?

My Excel/VBA skills are next to non-existent. Any suggestions is appreciated.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try adding this line of code before your Set statement...

Code:
Range("A3").CurrentArray.ClearContents
 
Upvote 0
Awesome! That works like a charm. Only thing I need to make sure that CurrentArray is really valid on that cell before calling it.
 
Upvote 0
Code:
    With Range("A3")
        If .HasArray Then .CurrentArray.ClearContents
    End With
 
Upvote 0

Forum statistics

Threads
1,215,100
Messages
6,123,086
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top