简体   繁体   中英

Selecting every second cell in excel vba

I'm trying to select the highlighted cells

在这里显示 .

The range extends from A2 to EL2 so 72 in total.

I think I need to create a vector that is [2,4,6...] then use that result to select the cells.

Currently my code is not working and is...

range(cells(2, 2*(1 To 72))).select

I'm stuck and need some help please.

Something like this should assist, might not be the easiest way..

Sub sel()

Dim l As Long
Dim r As Range

For l = 2 To 22 Step 2
    If r Is Nothing Then
        Set r = Cells(2, l)
    Else
        Set r = Union(r, Cells(2, l))
    End If
Next l

r.Select

End Sub

This would work:

Dim rng_exp As Range, x As Integer
Set rng_exp = Cells(2, 2)

For x = 2 To 72
    Set rng_exp = Application.Union(rng_exp, Cells(2, 2 * (x)))
Next

rng_exp.Select

In simple terms, use a loop:

Sub dural()
    Dim r As Range

    Set r = Range("B2")
    For i = 4 To 142 Step 2
        Set r = Union(r, Cells(2, i))
    Next i
    r.Select
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM