Looping through a range of multiple columns

Mufflon

New Member
Joined
Sep 14, 2015
Messages
22
Hi!

I couldn't get this to work. I want to right-align the first 3 columns, then I want to do the same thing again, but 5 columns off to the right and so on. I tried the below code, but it seems like it just repeats the align on the first 3 columns? I tried offseting it, but I didn't take :(

Any ideas? Really appreciate it!

Code:
    For k = 1 To n
        Range(Columns(1), Columns(3)).HorizontalAlignment = xlRight
        ActiveCell.Offset(0, 5).Select
    Next k
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
untested but try
Code:
For k = 1 To n step 8
        Range(Columns(k), Columns(k+3)).HorizontalAlignment = xlRight
Next k
 
Upvote 0
You've hardcoded the columns in your loop - that's why it is only affecting the first 3 columns.

Code:
For k = 1 To n
        Range(Columns([B][COLOR="#FF0000"]1[/COLOR][/B]), Columns([B][COLOR="#FF0000"]3[/COLOR][/B])).HorizontalAlignment = xlRight
        ActiveCell.Offset(0, 5).Select
    Next k

Try this code:

Code:
Public Sub RightAlignColumns()
Dim i       As Long, _
    LC      As Long
    
Application.ScreenUpdating = False

LC = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LC Step 8
    Range(Cells(1, i), Cells(1, i + 2)).EntireColumn.HorizontalAlignment = xlRight
Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0
You alignment command is not dependent on the loop or the active cell. You are explicitly choose to do columns 1-3 over and over again.

Maybe something like:
Code:
For k = 0 to (n - 1)
    Range(Columns(1 + (5 * k), Columns(3 + (5 * k)).HorizontalAlignment = xlRight
Next k
 
Last edited:
Upvote 0
You alignment command is not dependent on the loop or the active cell. You are explicitly choose to do columns 1-3 over and over again.

Maybe something like:
Code:
For k = 0 to (n - 1)
    Range(Columns(1 + (5 * k), Columns(3 + (5 * k)).HorizontalAlignment = xlRight
Next k

Thank you! This worked with two added parentheses :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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