Search code examples
excelvbaexcel-2013

VBA Finding Last Row with Value in Table Column


I have a table with several items in it. These items all have a code, and they will repeat themselves, row by row. I mean to single out the last 'repetition' of a item in a table, i need to have the address or row reference, something i can work with so i can later bring it to a new table, where i'll make a monthly overview of the current status of our items, present in the first table. And I've almost got it, the following code:

'codTeste is a integer in this example
codTeste = Range("AE:AE").Find(what:="REST0300", after:=Range("AE1"), searchdirection:=xlPrevious).Row

Does work but isn't there a way like so:

codTeste = Range(tblDesc.DataBodyRange.Address).Find(what:="REST0300", after:=Range(TheFirstCellOfTheRelevantColumnDataBodyRange), searchdirection:=xlPrevious).Row
    'If I do this it'll throw me a mismatch error

the point would be to make it more dynamic, so as to not restrict the find method to a very specific range parameter...


Solution

  • dim inCol as integer

    inCol = Worksheets("Sheet1").Cells.Find(What:="Product Code", After:=Worksheets("Sheet1").Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column

    I can't comment, but this will find the column number given the name @steps