VBA Select Column with Header but stop selection at specified Row

solved

Hello again friends

I'd like to select columns with a specific header and then stop at a certain row (like 'A1000". I'm doing this via macro and found one that's able to find and select columns based on the name of the column. However, it selects the entire column until the end of time, this is bad because once I paste my formula in, my laptop would suddenly take off like a helicopter and crash.

Anyway, is there a way for me to add a stop row for the selection?

Heres the partial code that I'm using:

Select Named Columns

xStr = "Name"
Set xRg = Range("A1:EG1").Find(xStr, , xlValues, xlWhole, , , True)
If Not xRg Is Nothing Then
    xFirstAddress = xRg.Address
    Do
        Set xRg = Range("A1:EG1").FindNext(xRg)
        If xRgUni Is Nothing Then
            Set xRgUni = xRg
        Else
            Set xRgUni = Application.Union(xRgUni, xRg)
        End If
    Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)
End If
xRgUni.EntireColumn.Select

End Sub

Posted Dec 23, 2019, 4:06 AM UTC

13
semicolonsemicolon993•

Hi boxedmachine. Try replacing xRgUni.EntireColumn.Select with

For Each cel In xRgUni
   Set xRgUni = Application.Union(xRgUni, cel.Resize(1000, 1))
Next cel
xRgUni.Select

Replace 1000 with however many rows you'd like to have selected.

3
boxedmachineOP•

Solution Verified!

Thank you ;; this works perfectly!

2
Clippy_Office_AsstMOD•

You have awarded 1 point to semicolonsemicolon

^I ^am ^a ^bot, ^please ^contact ^the ^mods ^for ^any ^questions.

1