Excel 选择直到最后使用的行的范围

Excel 选择直到最后使用的行的范围,excel,vba,Excel,Vba,我正在尝试选择一个范围,直到工作表中最后使用的行。我目前有以下情况: Sub Select_Active_Down() Dim lr As Long lr = ActiveSheet.UsedRange.Rows.Count If Cells(ActiveCell.Row, ActiveCell.Column) = Cells(lr, ActiveCell.Column) Then MsgBox "There isn't any data to

我正在尝试选择一个范围,直到工作表中最后使用的行。我目前有以下情况:

Sub Select_Active_Down()
    Dim lr As Long
    lr = ActiveSheet.UsedRange.Rows.Count
    If Cells(ActiveCell.Row, ActiveCell.Column) = Cells(lr, ActiveCell.Column) Then
        MsgBox "There isn't any data to select."
    Else
        Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(lr, ActiveCell.Column)).Select
        Cells(lr, ActiveCell.Column).Activate
    End If

End Sub

问题是我需要选择多个列,这将只选择活动范围的第一列。如何修改此选项以选择多个列而不仅仅是第一列?

如何选择整个区域?这可以在VBA中按如下方式完成:

Selection.CurrentRegion.Select

还可以选择整个阵列。为此,只需按Ctrl+G,选择
Special
,然后查看那边。

我会做稍微不同的操作。我将使用
.Find
to和最后一列(使用链接中显示的相同逻辑)来构建我的范围,而不是使用
选择|选择|活动单元格|使用dRange |活动表

这就是你想要的吗

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim rng As Range
    
    '~~> Change it to the relevant sheet
    Set ws = Sheet1
        
    With ws
        '~~> Check if there is data
        If Application.WorksheetFunction.CountA(.Cells) = 0 Then
            MsgBox "No Data Found"
            Exit Sub
        End If
        
        '~~> Find last row
        LastRow = .Cells.Find(What:="*", _
                  After:=.Range("A1"), _
                  Lookat:=xlPart, _
                  LookIn:=xlFormulas, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious, _
                  MatchCase:=False).Row
        
        '~~> Find last column
        LastColumn = .Cells.Find(What:="*", _
                     After:=.Range("A1"), _
                     Lookat:=xlPart, _
                     LookIn:=xlFormulas, _
                     SearchOrder:=xlByColumns, _
                     SearchDirection:=xlPrevious, _
                     MatchCase:=False).Column
                     
        '~~> Construct your range
        Set rng = .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
        
        '~~> Work with the range
        With rng
            MsgBox .Address
            '
            '~~> Do what you want with the range here
            '
        End With
    End With
End Sub

Range.Resize()可能就是您要查找的内容。为什么需要
。选择
?除非有具体的使用原因,否则它通常只会带来问题。看见