vba Range("X65536").End(xlUp).Row 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18263785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:47:26  来源:igfitidea点击:

Range("X65536").End(xlUp).Row is not working

excelvba

提问by user2683996

Hi I need help with this one line in my VBA:

嗨,我需要 VBA 中这一行的帮助:

lastrow = wsTarget.Range("X65536").End(xlUp).Row
MsgBox lastrow

wsTargetis the worksheet i am working on, I want to know what is the last row of data. By looking at the sheet, the last line should be 9; however, it keeps returning 1 to me. Am I doing something wrong?

wsTarget是我正在处理的工作表,我想知道最后一行数据是什么。查看表格,最后一行应该是 9;但是,它一直返回 1 给我。难道我做错了什么?

回答by tigeravatar

It's a little bit longer to type, but perhaps the Range.Find method will work for you:

输入时间有点长,但也许 Range.Find 方法对你有用:

lastrow = wsTarget.Columns("X").Find("*", wsTarget.Range("X1"), xlValues, SearchDirection:=xlPrevious).Row
MsgBox lastrow

回答by PatricK

In future, try avoid determine the last row by moving up from a defined cell. Especially 65536 which is the max row for Excel 2003 can handle (old code!). We should make codes to be version independent!

将来,尽量避免通过从定义的单元格向上移动来确定最后一行。特别是 65536,这是 Excel 2003 可以处理的最大行(旧代码!)。我们应该让代码与版本无关!

You will be good with below code for all future versions of office for what you are trying to find:

对于您要查找的所有未来版本的 Office,您将使用以下代码:

' Start going up from next row of last cell
lastrow = wsTarget.Cells.SpecialCells(xlLastCell).row + 1
lastrow = wsTarget.Range("X" & lastrow).End(xlUp).Row

回答by pnuts

Based on "the last row of data" not necessarily coinciding with ColumnX maybe the following courtesywould suit:

基于“最后一行数据”不一定与 ColumnX 重合,以下礼貌可能适合:

Sub Last_Real_Populated_Row()
    ActiveCell.SpecialCells(xlLastCell).Select
    LastR = ActiveCell.Row
    LastC = ActiveCell.Column
    LastRealC = 1
    For Counter = LastR To 1 Step -1
        Range(Cells(Counter, LastC), Cells(Counter, LastC)).Select
        Selection.End(xlToLeft).Select
        If Not IsEmpty(ActiveCell.Value) Then
            LastRealR = ActiveCell.Row
            Exit For
        End If
    Next
End Sub