Written by Allen Wyatt (last updated July 16, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
Jonathan is creating a macro and needs to fill a range of cells with values. For instance, if he needs to fill the range A1:C1, it currently takes three statements to fill that range:
Range("A1") = "Test1" Range("B1") = "Test2" Range("C1") = "Test3"
He wonders if there is a way to fill them in a single statement, similar to the following:
Range("A1:C1") = ("Test1","Test2","Test3")
Jonathan's desired syntax is close, but it won't work. Here's how it will work:
Range("A1:C1") = Array("Test1","Test2","Test3")
Note the use of the Array statement, which tells VBA that what follows should be considered a sequence of values to be used in the sequence of cells at the left of the operator. Interestingly enough, you could stuff values into variables and also use the Array statement, as shown here:
sOne = "Apples" sTwo = "Oranges" sThree = "Artichokes" Range("A1:C1") = Array(sOne, sTwo, sThree)
You can also work with straight variables, if you prefer:
Dim sMyStrings(2) As String sMyStrings(0) = "Apples" sMyStrings(1) = "Oranges" sMyStrings(2) = "Artichokes" Range("A1:C1") = sMyStrings
The above code could also be rewritten, as follows:
Dim sMyStrings(2) As String sMyStrings = Array("Apples", "Oranges", "Artichokes") Range("A1:C1") = sMyStrings
Finally, if you wanted to have the values placed into a single column rather than in a row, you would need to use the Transpose function, in this manner:
Range("A1:A3") = Application.Transpose(Array("Test1","Test2","Test3"))
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11702) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Filling a Range of Cells with Values.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
As your macro is processing information, there will doubtless be times that it will need to compare information in ...
Discover MoreWhen processing information in a macro, you often need to select different cells relative to the currently selected ...
Discover MoreDo you get tired of the dialog box that says "do you want to enable macros" that is displayed when you open a workbook. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-08-02 11:49:05
Mike D.
@J.Woolley
Ahhh, a little more has crept into my gray matter.
Allen's code had the issue, not me. I changed the DIM to Variant and deleted the (2) from the length and it worked perfectly.
Thank you
--------------------------------------------------------------------------------------------
Sub mike_2()
Dim sMyStrings() As Variant
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings
End Sub
--------------------------------------------------------------------------------------------
2022-08-02 10:28:34
J. Woolley
@Mike D.
The VBA Array(...) function must be assigned to a Variant variable or Variant array of undeclared size, so this is not allowed for two reasons:
Dim A(2) as String
A = Array("Apples", "Oranges", "Artichokes")
Note that Variant is the default type, so these are OK:
Dim A() As Variant, B As Variant, C, D()
A = Array("Apples", "Oranges", "Artichokes")
B = Array(1, 2, 3)
C = Array("Apples", "Oranges", "Artichokes")
D = Array(1, 2, 3)
Also, be careful with Option Base; the default is 0, but it might have been declared as 1 in your module. The qualified function VBA.Array(...) is not affected by Option Base, so this always creates an array like A(0 to 2):
A = VBA.Array("Apples", "Oranges", "Artichokes")
But this might create A(0 to 2) or A(1 to 3) depending upon Option Base:
A = Array("Apples", "Oranges", "Artichokes")
See https://sites.google.com/view/MyExcelToolbox/
2022-08-02 08:32:30
Mike D.
Thank you Peter and Willy.
I have been able to get it to work with the other code Allen posted, I was trying to figure out what I was doing wrong.
I experimented with the code and figured out a few things. I will try your suggestions for fun. Learn, learn learn.
What I am hoping is to figure out the syntax for the variable = Array(data, data, data).
I feel it should work but there is something simple I am missing.
Thanks again
2022-08-02 06:46:50
Willy Vanhaelen
@Peter Atherton
You can even write it as a one liner:
Sub t()
Range("A1:C1") = Split("Apples Oranges Artchokes")
End Sub
2022-08-02 06:18:24
Peter Atherton
Mike D
Try this
Sub t()
Dim s
Dim sMyStrings As Variant
s = "Apples, Oranges,Artichokes"
sMyStrings = Split(s, ",")
Range("A1:C1") = sMyStrings
End Sub
2022-08-01 15:17:25
Mike D.
I copied your code from above and ran into an error I cannot resolve.
Dim sMyStrings(2) As String
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings
Compile Error:
Can't assign to array
The editor highlights the sMyStrings variable as the culprit.
What am I missing? (Running 365)
2022-08-01 14:23:57
Mike D.
I copied your code from above and ran into an error I cannot resolve.
Dim sMyStrings(2) As String
sMyStrings = Array("Apples", "Oranges", "Artichokes")
Range("A1:C1") = sMyStrings
Compile Error:
Can't assign to array
The editor highlights the sMyStrings variable as the culprit.
What am I missing? (Running 365)
2022-07-16 10:14:03
J. Woolley
In newer versions of Excel with dynamic arrays, here is yet another way using curly brackets like {...}.
For 3 columns in one row:
Range("A1").Formula2 = "={""Test1"",""Test2"",""Test3""}"
For 3 rows in one column:
Range("A1").Formula2 = "={""Test1"";""Test2"";""Test3""}"
Each results in an array formula of text constants instead of the Tip's array of constant text values.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments