Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code to select a series of noncontiguous columns I need a macro that selects non

ID: 3561578 • Letter: C

Question

Code to select a series of noncontiguous columns

I need a macro that selects noncontiguous columns.
If selected manually, here are the steps:

Select F21
If cell F21 is not blank, select Columns F:H, if it is blank, End Sub

Move 7 columns to the right to cell M21
If cell M21 is not blank, additionally select Columns M:O, if it is blank, End Sub

Move 7 columns to the right to cell T21
If cell T21 is not blank, additionally select Columns T:V, if it is blank, End Sub

Etc.

The selection and column spacing is always evenly spaced (7 Columns, Row 21), as above.
At the end of the macro all of the following columns will be selected:
F:H
M:O
T:V
Etc.   

Explanation / Answer

^#&3

It is rarely necessary to select (or activate) anything, but to do what you seem to want to do, try:

==========================================================

Option Explicit
Sub SelectMultiple()
    Dim ColNum As Long
    Dim myRange As Range
   
ColNum = 6
Do Until Cells(21, ColNum) = ""
    If myRange Is Nothing Then
        Set myRange = Cells.Columns(ColNum).Resize(columnsize:=3)
    Else
        Set myRange = Union(myRange, Cells.Columns(ColNum).Resize(columnsize:=3))
    End If
    ColNum = ColNum + 7
Loop

myRange.Select
       
Debug.Print myRange.Address '<-- for debugging
   
End Sub

=====================================

^_^