Select Alternating Rows

C

Chad Brantly

I need to write a macro to select alternating rows. I have been able
to do it for a set number of rows by specifying row numbers in the
macro, but I am having problems doing it for an entire sheet that is
VERY long. Does anyone know an easy way to do this?
 
J

J.E. McGimpsey

One way:

Const nSTART As Long = 1
Dim rSelect As Range
Dim i As Long
Set rSelect = Cells(nSTART, 1)
For i = nSTART + 2 To ActiveSheet.UsedRange.Rows.Count Step 2
Set rSelect = Union(rSelect, Cells(i, 1))
Next i
rSelect.EntireRow.Select

Change nSTART to 2 to select even rows.

Note, however, that you almost never need to select anything in
macros. For one thing, Select is very slow. If you post back with
what you're trying to do, perhaps you can get advice on a more
efficient macro.
 
C

Chad Brantly

Thank you so much!! I have been struggling with this for a couple of
weeks. We are using this for some genetic research we are doing
(Genotyping) and we need to apply different things to alternating rows
quite frequently. Since what needs to be done is always changing, I
thought the easiest thing would be to create a macro to select
alternating rows. Am I mistaken?

Thanks again for your help!
 
J

J.E. McGimpsey

Thank you so much!! I have been struggling with this for a couple of
weeks. We are using this for some genetic research we are doing
(Genotyping) and we need to apply different things to alternating rows
quite frequently. Since what needs to be done is always changing, I
thought the easiest thing would be to create a macro to select
alternating rows. Am I mistaken?

Depends - if what you do is always different, I'd guess a selection
macro is best.

OTOH, if you're always doing one of, say, six things, i'd be
inclined to write six macros that did the whole step and create a
toolbar with six buttons to call them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top