简体   繁体   English

VBA在表列中查找具有值的最后一行

[英]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... 关键是使其更具动态性,以免将find方法限制为非常具体的范围参数...

This works for me: 这对我有用:

Dim trng As Range, f As Range, inCol As Long
Set trng = ActiveSheet.ListObjects("Table1").DataBodyRange

useCol = 2

Set f = trng.Columns(inCol).Find(what:="77", LookIn:=xlValues, lookat:=xlWhole, _
        searchdirection:=xlPrevious, after:=trng.Columns(inCol).Cells(1))

If Not f Is Nothing Then Debug.Print f.Row

dim inCol as integer 将inCol调为整数

inCol = Worksheets("Sheet1").Cells.Find(What:="Product Code", After:=Worksheets("Sheet1").Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column inCol = Worksheets(“ Sheet1”)。Cells.Find(What:=“产品代码”,之后:= 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 我无法发表评论,但这将找到名称为@steps的列号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM