Loop through Variant filling Range

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to learn more about looping and using a variant.

How close am I to getting the result

Copy of 2021_CertReadinessReport_ASE_AudiSouthern_10162021.xls
A
1Area 80
2Area 82
3Area 82a
4Area 82b
5Area 83
6Area 83a
Sheet1


VBA Code:
Sub yetanothertest()
Dim rng As Range, m As Long, Var As Variant
Var = Array("Area 80", "Area 82", "Area 82a", "Area 82b", "Area 83", "Area 83a")
Set rng = Range("A1")
For m = 1 To 6
Range("A" & m).Value = Var
Next m
End Sub

Thank you
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Rich (BB code):
For m = 0 To 5
Range("A" & m + 1).Value = Var(m)

VBA help must read : Array, LBound & UBound …​
Your variable rng is useless !​
A better way than a loop : [A1:A6] = Application.Transpose(Var) …​
 
Last edited:
Upvote 0
Or​
Rich (BB code):
For m = 1 To 6
Range("A" & m).Value = Var(m - 1)
 
Upvote 0
Rich (BB code):
For m = 0 To 5
Range("A" & m + 1).Value = Var(m)

VBA help must read : Array, LBound & UBound …​
I hit F1 for help, couldn't understand it. Thanks for the pointers.
VBA Code:
Sub yetanothertest()
Dim rng As Range, m As Long, Var As Variant
Var = Array("Area 80", "Area 82", "Area 82a", "Area 82b", "Area 83", "Area 83a")
'Set rng = Range("A1")
For m = 1 To 5
Range("A" & m).Value = Var(m)
Next m
End Sub
 
Upvote 0
See post #3 !​
The direct allocation : [A1:A6] = [{"Area 80";"Area 82";"Area 82a";"Area 82b","Area 83","Area 83a"}] …​
 
Upvote 0
Or​
Rich (BB code):
For m = 1 To 6
Range("A" & m).Value = Var(m - 1)
if m = 1 to 6, then we start with range A1, and going off the Array why do you use -1? Wouldn't m be "Area 80"? I don't understand using a negative, what that accomplishes. I'll have to play around with the numbers and see what the output is. Thank you
 
Upvote 0
Correction for the direct allocation :​
Rich (BB code):
[A1:A6] = [{"Area 80";"Area 82";"Area 82a";"Area 82b";"Area 83";"Area 83a"}]

As it's explain in the VBA help of Array statement but you can also understand why​
just taking a glance during the execution in the VBE Locals window to see how is your Var array …​
 
Upvote 0
See post #3 !​
The direct allocation : [A1:A6] = [{"Area 80";"Area 82";"Area 82a";"Area 82b","Area 83","Area 83a"}] …​
This so simple, thank you.
I did have to change the ; to a , for it to work. Maybe it's my version of Excel?
 
Upvote 0

No my bad 'cause I had no time to edit before the site maintenance was launched …​
See my post #7.​
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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