Results 1 to 2 of 2

Thread: Select non-consecutive columns with a VBA macro

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021

    Select non-consecutive columns with a VBA macro

    I am trying to write a VBA code that automatically detects the end of columns (until it reached a blank cell). Then selects the columns of the user-selected cells.

    Here is the sub I am writing:

    Do While IsEmpty(ActiveCell.Offset(n, 0)) = False
    n = n + 1
    Range(ActiveCell, ActiveCell.Offset(n - 1, 0)).Select

    This only works for one cell that is active. As I said, I want to select columns of all selected cells of a row. Example: If I have selected A1, D1 and F1 then, I want to select data below all these 3 cells with a single click macro. I searched a lot but not able to find any such VBA online.

    Last edited by mobilepoker; Jun 6th, 2021 at 07:26 AM.

  2. #2
    Join Date
    Dec 2004

    Re: Select non-consecutive columns with a VBA macro

    i would believe this should do what you want
    Dim rng As Range, cel As Range
    Set rng = Selection
    For Each cel In Selection
        Set rng = Union(rng, cel.Resize(Cells(Rows.Count, cel.Column).End(xlUp).Row - cel.Row + 1))
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width