Loop adding text to range of cells across columns

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I sort of started out what I'm trying to do, am I close :)

VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("Parts", "Sales", "Service", "Technical", "Total")
Set rng = Range("C2")

For i = 0 To 5

rng.Offset(0, i + 1) = var & Chr(10) & "Certified"
rng.Offset(0, i + 2) = var & Chr(10) & "Active"
rng.Offset(0, i + 3) = var & Chr(10) & "Cert%"

Next i



The final result should look like this
Book4
CDEFGHIJKLMNOPQ
2Parts CertifiedParts ActiveParts Cert %Sales CertifiedSales ActiveSales Cert %Service CertifiedService ActiveService Cert %Technical CertifiedTechnical ActiveTechnical Cert %Total CertifiedTotal ActiveTotal Cert %
Sheet1
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try it like
VBA Code:
For i = 0 To UBound(var)

rng.Offset(0, i * 3) = var(i) & Chr(10) & "Certified"
rng.Offset(0, i * 3 + 1) = var(i) & Chr(10) & "Active"
rng.Offset(0, i * 3 + 2) = var(i) & Chr(10) & "Cert%"

Next i
 
Upvote 0
Perhaps you could give this a try ...
VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("Parts", "Sales", "Service", "Technical", "Total")
Set rng = Range("C2")

For i = 0 To 12 Step 3

    rng.Offset(0, i + 1) = var(i / 3) & Chr(10) & "Certified"
    rng.Offset(0, i + 2) = var(i / 3) & Chr(10) & "Active"
    rng.Offset(0, i + 3) = var(i / 3) & Chr(10) & "Cert%"

Next i
 
Upvote 0
Solution
Try it like
VBA Code:
For i = 0 To UBound(var)

rng.Offset(0, i * 3) = var(i) & Chr(10) & "Certified"
rng.Offset(0, i * 3 + 1) = var(i) & Chr(10) & "Active"
rng.Offset(0, i * 3 + 2) = var(i) & Chr(10) & "Cert%"

Next i
Thank you, these certainly is condensed. I'm unfamiliar with Ubound and will need to do some reading.
 
Upvote 0
Perhaps you could give this a try ...
VBA Code:
Dim i As Long
Dim rng As Range
Dim var As Variant

var = Array("Parts", "Sales", "Service", "Technical", "Total")
Set rng = Range("C2")

For i = 0 To 12 Step 3

    rng.Offset(0, i + 1) = var(i / 3) & Chr(10) & "Certified"
    rng.Offset(0, i + 2) = var(i / 3) & Chr(10) & "Active"
    rng.Offset(0, i + 3) = var(i / 3) & Chr(10) & "Cert%"

Next i
I was so close, adding Step is new for me. I am happy though that I was able to put most of it together.

Thanks for the help.
 
Upvote 0
I am happy though that I was able to put most of it together.
It is always most satisfying when you get things done yourself. Well done!

Glad we could help and thanks for letting us know.
 
Upvote 0
Thank you, these certainly is condensed. I'm unfamiliar with Ubound and will need to do some reading.
You use UBOUND when dealing with arrays. It is literally "Upper Bound".
When you use that, you don't need to hard-code the size of the array. It makes it dynamic, saying "up to the last entry".
It is nice because you can then add/remove values to your array, and not have to adjust the loops in your code.

See: UBound function (Visual Basic for Applications)
 
Upvote 0
You use UBOUND when dealing with arrays. It is literally "Upper Bound".
When you use that, you don't need to hard-code the size of the array. It makes it dynamic, saying "up to the last entry".
It is nice because you can then add/remove values to your array, and not have to adjust the loops in your code.

See: UBound function (Visual Basic for Applications)
That makes sense. Thanks, I'll have to play around with that. I'm just getting used to how to writing in one way.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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