为UsedRange Excel VBA选择多列数据

时间:2019-01-24 13:50:43

标签: excel vba

希望有人可以提供帮助。 我正在尝试从4列中选择数据作为一个UsedRange的一部分。

我不希望选择整个列,而只选择四个列(C,E,F,H)中的每个数据作为一个范围。如果工作表中没有空隙,我可以轻松地使用`Range(C1:H5)等。

每列具有不同数量的行,这些行会定期更改。

Example

3 个答案:

答案 0 :(得分:2)

您可以使用Union将范围合并为一个

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

不过,就我个人而言,我会将它们保留在单独的范围内,并在需要时对它们进行相同的操作。

答案 1 :(得分:1)

您可以使用Union method创建一个多区域Range,如下所示:

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

请注意,Worksheet.UsedRange完全不同,它是Excel设置的只读对象

答案 2 :(得分:1)

使用循环和union()

的动态解决方案

这将选择每列的确切使用范围。缺点是此选择不允许复制/粘贴(请参阅替代解决方案)。

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

enter image description here


替代:选择最大使用范围

此功能的优点是您可以复制/粘贴,而第一个选择不允许复制/粘贴。

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

enter image description here


或更短

可能不如长版准确。

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

enter image description here