-
Jun 6th, 2021, 03:45 AM
#1
Thread Starter
New Member
Select non-consecutive columns with a VBA macro
Hi,
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
Loop
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.
Thanks
Last edited by mobilepoker; Jun 6th, 2021 at 07:26 AM.
-
Jun 7th, 2021, 05:24 AM
#2
Re: Select non-consecutive columns with a VBA macro
i would believe this should do what you want
Code:
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))
Next
rng.Select
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
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|