vba - loop offset merged columns

noidea23

New Member
Joined
Feb 16, 2022
Messages
28
Office Version
  1. 2021
Platform
  1. Windows
hello! i have to loop through specific columns because some columns are merged. In the code below, i am not sure how to resolve the part on "For j = 1,5,7,13,17". i need the code to run only columns 1,5,7,13,17. Any help will be greatly appreciated!

For k = 1 to N # to loop rows
For j = 1, 5,7,13,17 # to loop specific columns because some are merged
If IsNumeric(ActiveCell.Offset(k, j)) Then
Do something
End If
Next j
Next k
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi! For loops are fundamentally not designed for irregular iteration. You could:
  1. Use if statements or a select case statement to increment the index depending on the current value or index
  2. If you truly will always only do 5 iterations, just ditch the loop entirely and repeat the code operation 5 times (or better, move the code to its own sub and pass a parameter from for the row, and call it 6 different times with a different parameter).
If you go with 1, you might end up with something like
VBA Code:
    For k = 1 To n
        For j = 1 To 17
            Debug.Print j
            Select Case j
                Case 1
                    j = j + 3 'Increment one less than we want because the loop adds +1
                Case 5
                    j = j + 1
                Case 7
                    j = j + 5
                Case 13
                    j = j + 3
            End Select
        Next j
    Next k
 
Upvote 0

Forum statistics

Threads
1,224,885
Messages
6,181,574
Members
453,055
Latest member
cope7895

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