lastrow and excel table.

后端 未结 4 1324
失恋的感觉
失恋的感觉 2020-12-06 08:23

I have tried to define a lastrow code that would return the last row value of the last non empty cell in a excel table. (ITS FORMATTED AS A TABLE)

My exceltable have

相关标签:
4条回答
  • 2020-12-06 08:59

    try this

     Dim DataRange As Range
    
    Set DataRange = Range("A1:M" & LastRow)
    
    0 讨论(0)
  • 2020-12-06 09:07

    You will have issue if there is data below the excel table on the sheet. It's always better to refer the table column while finding the last row in an excel table.

    Sub FindLastRowInExcelTableColAandB()
    Dim lastRow1 As Long, lastRow2 As Long
    Dim ws As Worksheet
    Set ws = Sheets("DataÖnskemål")
    'Assuming the name of the table is "Table1"
    lastRow1 = ws.ListObjects("Table1").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lastRow2 = ws.ListObjects("Table1").Range.Columns(2).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End Sub
    
    0 讨论(0)
  • 2020-12-06 09:13

    Try the code below to get the last row in Column A and Column B from a Table (ListObject):

    Option Explicit
    
    Sub LastRowTable()
    
    Dim Tbl As ListObject
    Dim LastRow2  As Long, LastRow3 As Long
    
    ' modify "Table1" to your table's Name
    Set Tbl = ThisWorkbook.Worksheets("DataÖnskemål").ListObjects("Table1")
    
    LastRow3 = Tbl.ListColumns(1).Range.Rows.Count '<-- last row in Column A in your Table
    LastRow2 = Tbl.ListColumns(2).Range(LastRow3, 1).End(xlUp).Row '<-- last row  with data in Column B in your Table
    
    End Sub
    
    0 讨论(0)
  • 2020-12-06 09:14

    Finding last rows of tables is a bit fiddly, especially as you often need to cater for the case that a user has filtered the data. A loop with multiple checks might suit you better as you can adjust it to your own needs for the data within the table.

    You also don't mention whether you can be certain that the last row is indeed a table.

    In view of these points, perhaps the .Find function will suit you as it will find any non-empty cell whether in a table or not and whether hidden or not (though it doesn't cope with a filtered table). (It's not quite true to say "any non-empty cell", as a null string, for example, wouldn't be picked up, but maybe these exceptions won't trouble you). Anyhow your code could be:

    With Sheet1
        lastRow1 = .Columns(1).Find(What:="*", _
                                    After:=.Columns(1).Cells(1), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Row
    
        lastRow2 = .Columns(2).Find(What:="*", _
                                    After:=.Columns(2).Cells(1), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Row
    End With
    
    0 讨论(0)
提交回复
热议问题