简体   繁体   中英

Select multiple columns of data for a UsedRange Excel VBA

Hope someone can help. I am trying to select data from 4 columns to be part of one UsedRange .

I do not wish to select the entire column, just the data in each of the four columns (C,E,F,H) as one range. If there were no gaps in the sheet I could easily use `Range(C1:H5) etc.

Each column has different number of rows which regularly change.

例

You can use Union to combine ranges into one

Dim rng As Range
Dim CLastRow As Long, ELastRow As Long, FLastRow As Long, HLastRow As Long

' Update with your Sheet reference
With YourSheet
    CLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
    ELastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
    FLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
    HLastRow = .Cells(.Rows.Count, "H").End(xlUp).Row

    Set rng = Union(.Range("C1:C" & CLastRow), _
                    .Range("E1:E" & ELastRow), _
                    .Range("F1:F" & FLastRow), _
                    .Range("H1:H" & HLastRow))
End With

Personally though, I'd keep them as separate ranges and just do the same operation on each of them if needed.

You can create a multi-area Range with the Union method , like so:

Set Combined_Rage = Union(Range1, Range2) 'You can have up to 30 different Ranges

Please note that Worksheet.UsedRange is something completely different, and is a read-only object set by Excel

A dynamic solution using a loop and union()

This selects the exact used range of each column. Disadvantage is that this selection doesn't allow copy/paste (see alternative solution).

Option Explicit

Public Sub SelectUsedRangeOfSeveralColumns()     
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    Dim ArrCols As Variant 'define which columns to select
    ArrCols = Array("C", "E", "F", "H")

    Dim UnifiedRange As Range 'we collect the ranges here

    Dim Col As Variant
    For Each Col In ArrCols
        If UnifiedRange Is Nothing Then 'first range
            Set UnifiedRange = ws.Range(Col & "1", ws.Cells(ws.Rows.Count, Col).End(xlUp))
        Else 'all following ranges
            Set UnifiedRange = Union(UnifiedRange, ws.Range(Col & "1", ws.Cells(ws.Rows.Count, Col).End(xlUp)))
        End If
    Next Col

    'this is just to visualize what the UnifiedRange contains
    UnifiedRange.Select
End Sub

在此处输入图片说明


Alternative: Maximal used range is selected

Advantage of this one is that you can copy/paste while the first selection doesn't allow copy/paste.

Option Explicit

Public Sub SelectMAXUsedRangeOfSeveralColumns()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")

    Dim ArrCols As Variant
    ArrCols = Array("C", "E", "F", "H")

    Dim LastRow As Long
    Dim UnifiedRange As Range

    Dim Col As Variant
    For Each Col In ArrCols
        LastRow = Application.Max(LastRow, ws.Cells(ws.Rows.Count, Col).End(xlUp).Row)
        If UnifiedRange Is Nothing Then
            Set UnifiedRange = ws.Columns(Col)
        Else
            Set UnifiedRange = Union(UnifiedRange, ws.Columns(Col))
        End If
    Next Col

    Set UnifiedRange = Intersect(UnifiedRange, ws.Range(ArrCols(0) & "1", ws.Cells(LastRow, ArrCols(UBound(ArrCols)))))

    'this is just to visualize what the UnifiedRange contains
    UnifiedRange.Select
End Sub

在此处输入图片说明


Or even shorter

Might be not as accurate as the long version.

Option Explicit

Public Sub SelectMAXUsedRangeOfSeveralColumns()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")

    Dim UnifiedRange As Range
    Set UnifiedRange = Intersect(ws.UsedRange, ws.Range("C:C,E:E,F:F,H:H"))


    'this is just to visualize what the UnifiedRange contains
    UnifiedRange.Select
End Sub

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM