Each third column select

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Each third column select

Post by Rudi »

Quick one...
How do I get code to select ever third column in the used range?
TX

Code: Select all

Sub EachThird()
Dim rRng As Range, i As Long
Set rRng = ActiveSheet.UsedRange
For i = 1 To rRng.Columns.Count Step 2
    'Set rRng = Application.Union(??, rRng.Columns(i))
Next i
rRng.Select
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Each third column select

Post by HansV »

Like this:

Code: Select all

Sub EachThird()
    Dim rUsed As Range, rSelect As Range, i As Long
    Set rUsed = ActiveSheet.UsedRange
    For i = 1 To rUsed.Columns.Count Step 3
        If rSelect Is Nothing Then
            Set rSelect = rUsed.Columns(i)
        Else
            Set rSelect = Application.Union(rSelect, rUsed.Columns(i))
        End If
    Next i
    rSelect.Select
End Sub
We need a separate range to select, and the step should be 3 instead of 2 if you want to select every 3rd column.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Each third column select

Post by Rudi »

Ah....so I needed another range variable.
TX, works perfect. :)
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.