Select Cells using STEP in VBA

jjlafond

Board Regular
Joined
Jul 17, 2014
Messages
56
Hello,

I would like to select every other (or ever third/fourth) cell in a row using STEP (or a better method if there is one). Every other cell needs to be selected at the same time (similar to holding ctrl and clicking on multiple cells).


My end goal is to create a line graph (without copying the table and switching rows/columns). I plan on selecting every other cell in a row and setting the graph data range as those selected cells using vba. (The graph will be constantly updated as the human user enters more data into the long row of data.)

Any ideas on how to make this work?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hey There!

This sounds like a case where you might want to reconsider the structure of your data but nonetheless here is a naive looping solution to your issue. Note that it gets slow at about 1-2000 columns on my computer so if you are doing anything longer than that this is not the best approach.

Code:
Sub SelectNthCell(inputRange As Range, _
                    Optional inputN As Long = 2)
Dim i As Long
Dim rOutput As Range


'no error handling here
Set rOutput = inputRange.Cells(1)
For i = 1 + inputN To inputRange.Columns.Count Step inputN
    Set rOutput = Union(rOutput, inputRange.Cells(1, i))
Next i


'select cells
rOutput.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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