在Excel VBA中查找最后一行
我得到运行时错误1004:应用程序定义错误或对象定义错误在Excel VBA中查找最后一行,excel,vba,row,Excel,Vba,Row,我得到运行时错误1004:应用程序定义错误或对象定义错误 Set wsht = ThisWorkbook.Worksheets("Input Checklist") finalrow = wsht.Cells(wsht.Rows.Count, 1).End(x1Up).Row 错误与什么有关?可能是因为您在工作表级别录制了宏。您必须确保它位于项目/整个模块级别 在“项目”窗口的“模块”节点上单击鼠标右键,然后将宏粘贴到新模块中。还要确保工作表未处于保护模式,否则也会发生错误。您使用的是x1Up
Set wsht = ThisWorkbook.Worksheets("Input Checklist")
finalrow = wsht.Cells(wsht.Rows.Count, 1).End(x1Up).Row
错误与什么有关?可能是因为您在工作表级别录制了宏。您必须确保它位于项目/整个模块级别
在“项目”窗口的“模块”节点上单击鼠标右键,然后将宏粘贴到新模块中。还要确保工作表未处于保护模式,否则也会发生错误。您使用的是
x1Up
而不是xlUp
。比较它们的边到边<代码> 1L左边是代码中的1号,右边是字母L.你可能想考虑下面的函数。如果数据集的最后一行被隐藏,或者(可能)应用了过滤器,则上述方法将不起作用。下面的函数提供了实现相同结果的更可靠的方法
希望这有帮助Function LastRowColumn(sht As Worksheet, RowColumn As String) As Long
'PURPOSE: Function To Return the Last Row Or Column Number In the Active Spreadsheet
'INPUT: "R" or "C" to determine which direction to search
Dim rc As Long
Select Case LCase(Left(RowColumn, 1)) 'If they put in 'row' or column instead of 'r' or 'c'.
Case "c"
LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Case "r"
LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Case Else
LastRowColumn = 1
End Select
End Function
我没有把它当作宏。我把这个放在物体里面,在物体表里面,确实你是对的。