Find the last cell address using vba excel

前端 未结 4 520
我在风中等你
我在风中等你 2020-12-11 00:07

I have searched this site and it seems like all the answers just point to finding the row number of the cell.

I am trying to set a range so that it will go from

相关标签:
4条回答
  • 2020-12-11 00:35

    This will give the last row in a given column

    = Cells(Activesheet.Rows.Count, ColumnNumber).End(xlUp).Row (Fixed per @Gimp)

    you then have a reference you can use to add to the data - e.g if you want to look in column "A", then that would be columnnumber 1. feed that into the function, then you can use Cells(NumberReturnedFromFunction,ColumnNumber) to address that cell, and add .Address if you want the A1 style of address

    0 讨论(0)
  • 2020-12-11 00:51

    This will return the range object corresponding to the last filled in cell in column A

    Range("A:A").Find("*",Range("A1"),SearchDirection:=xlprevious)
    

    If you want the row number, use the following:

    Range("A:A").Find("*",Range("A1"),SearchDirection:=xlprevious).row
    
    0 讨论(0)
  • 2020-12-11 00:54

    Try using something like this:

    Activesheet.Cells(Activesheet.Rows.Count, "A").End(xlUp).Row
    

    You can replace Activesheet with references to a sheet's index # like Sheets(1) or the sheet name like Sheets("Sheet1")

    By using the Rows.Count it will check to see what the max rows are and be compatible across all versions of Excel.

    In the end you can use this within your range reference like this:

    Msgbox Sheets(1).Range("A" & Sheets(1).Cells(Sheets(1).Rows.Count, "A").End(xlUp).row).value
    

    But I'd probably rewrite that as

    With Sheets(1)
        Msgbox .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).row).value
    End With
    
    0 讨论(0)
  • 2020-12-11 00:54

    In case there are gaps in the data I'd avoid using xlDown so something like the following is fine. Try it in the immediate window:

    Activesheet.range("A1:A" & Activesheet.Cells(Excel.Rows.Count, 1).End(Excel.xlUp).Row).select
    
    0 讨论(0)
提交回复
热议问题