Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lastrow and excel table.

Tags:

excel

vba

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 values in COL A from 1 to 1005, COL B from 1 to 414 and in COL C from 414 to 1005.

What i want is to have one lastrow code that returns 414 and one that returns 1005. The problem i get is that because it is in a table. my codes

        lastrow3 = ThisWorkbook.Worksheets("DataÖnskemål").Range("A" & Rows.Count).End(xlUp).Row
        lastrow2 = ThisWorkbook.Worksheets("DataÖnskemål").Range("B" & Rows.Count).End(xlUp).Row

Both return 1005. Can i get around this with my table or is it a formating issue of some sort?

Best regards and thanks in advance /D

like image 713
DL1 Avatar asked Dec 05 '22 14:12

DL1


2 Answers

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
like image 179
Subodh Tiwari sktneer Avatar answered Dec 17 '22 20:12

Subodh Tiwari sktneer


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
like image 37
Ambie Avatar answered Dec 17 '22 20:12

Ambie