Step For statement

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Hi, so I know how to write a contiguous FOR Step statement, such as for x = 2 to 15 step 3 but I have some columns I want to skip some columns.

I want to apply to columns B-D then G-I and last L-N For x = 2 to 15 Step 3 does B,H,K,N -- help???
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
For x = 2 To 14   
    ..code..
    If x = 4 Then x = 6
    If x = 9 Then x = 11
Next x
 
Last edited:
Upvote 0
Roderick,

Couldn't you accomplish what you want by manipulating your x variable?

I'm thinking you could test for x = 4 or 9 before your Next statement:
The little test code below put the word test in Cols B-D, G-I and L-N.
By the way, just as FYI, Column N is Column # 14.

Code:
Sub skipcols()
Dim x As Long


For x = 2 To 14
    Cells(2, x).Value = "Test"
    If x = 4 Or x = 9 Then
        x = x + 2
    End If
Next


End Sub
 
Upvote 0
I want to apply to columns B-D then G-I and last L-N For x = 2 to 15 Step 3 does B,H,K,N -- help???
You could use a loop within a loop...
Code:
For Col = 2 To 12 Step 5
  For Off = 0 To 2
    Columns(Col).Offset(, Off).[COLOR="#0000FF"]Whatever Whatever[/COLOR]
  Next
Next
 
Last edited:
Upvote 0
Thanks for all the feedback, I ultimately did use a form of if x <> whatever and if x <> whatever then....
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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