Excel VBA select range at last row and column

前端 未结 3 934
甜味超标
甜味超标 2020-12-09 23:36

I\'m trying to create a macro that selects the range of last row and last column.

E.g. I want to select 1, 2, 3, 4 from my spreadsheet and then delete t

相关标签:
3条回答
  • 2020-12-09 23:56

    Is this what you are trying? I have commented the code so that you will not have any problem understanding it.

    Sub Sample()
        Dim ws As Worksheet
        Dim lRow As Long, lCol As Long
        Dim rng As Range
    
        '~~> Set this to the relevant worksheet
        Set ws = [Sheet1]
    
        With ws
            '~~> Get the last row and last column
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    
            '~~> Set the range
            Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol))
    
            With rng
                Debug.Print .Address
                '
                '~~> What ever you want to do with the address
                '
            End With
        End With
    End Sub
    

    BTW I am assuming that LastRow is the same for all rows and same goes for the columns. If that is not the case then you will have use .Find to find the Last Row and the Last Column. You might want to see THIS

    0 讨论(0)
  • 2020-12-10 00:04

    Another simple way:

    ActiveSheet.Rows(ActiveSheet.UsedRange.Rows.Count+1).Select    
    Selection.EntireRow.Delete
    

    or simpler:

    ActiveSheet.Rows(ActiveSheet.UsedRange.Rows.Count+1).EntireRow.Delete
    
    0 讨论(0)
  • 2020-12-10 00:08

    The simplest modification (to the code in your question) is this:

        Range("A" & Rows.Count).End(xlUp).Select
        Selection.EntireRow.Delete
    

    Which can be simplified to:

        Range("A" & Rows.Count).End(xlUp).EntireRow.Delete
    
    0 讨论(0)
提交回复
热议问题