如何查找包含特定列和特定工作表中数据的最后一行?如何查找包含Excel表格中包含宏数据的最后一行?
50
A
回答
40
如何:
Sub GetLastRow(strSheet, strColumn)
Dim MyRange As Range
Dim lngLastRow As Long
Set MyRange = Worksheets(strSheet).Range(strColum & "1")
lngLastRow = Cells(Rows.Count, MyRange.Column).End(xlUp).Row
End Sub
回复评论
这
Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
即使最后一行中只有一个单元格有数据,也会返回最后一个单元格的行号。
-2
Function LastRow(rng As Range) As Long
Dim iRowN As Long
Dim iRowI As Long
Dim iColN As Integer
Dim iColI As Integer
iRowN = 0
iColN = rng.Columns.count
For iColI = 1 To iColN
iRowI = rng.Columns(iColI).Offset(65536 - rng.Row, 0).End(xlUp).Row
If iRowI > iRowN Then iRowN = iRowI
Next
LastRow = iRowN
End Function
7
function LastRowIndex(byval w as worksheet, byval col as variant) as long
dim r as range
set r = application.intersect(w.usedrange, w.columns(col))
if not r is nothing then
set r = r.cells(r.cells.count)
if isempty(r.value) then
LastRowIndex = r.end(xlup).row
else
LastRowIndex = r.row
end if
end if
end function
用法:
? LastRowIndex(ActiveSheet, 5)
? LastRowIndex(ActiveSheet, "AI")
0
第一个函数将光标移动到列中最后一个非空行。 第二个函数打印该列的行。
Selection.End(xlDown).Select
MsgBox (ActiveCell.Row)
21
,您应该使用.End(xlup)
但不是使用65536你可能想使用:
sheetvar.Rows.Count
它为Excel 2007中,我相信有超过65536行
3
Public Function LastData(rCol As Range) As Range
Set LastData = rCol.Find("*", rCol.Cells(1), , , , xlPrevious)
End Function
这样
用法:?lastdata(activecell.EntireColumn).Address
3
下面是找到最后一行的解决方案,l ast列或最后一个单元格。它解决了它找到的列的A1 R1C1参考风格困境。希望我可以信任,但无法找到/记住我从哪里得到它,所以“谢谢!”无论是在哪里发布原始代码的人。
Sub Macro1
Sheets("Sheet1").Select
MsgBox "The last row found is: " & Last(1, ActiveSheet.Cells)
MsgBox "The last column (R1C1) found is: " & Last(2, ActiveSheet.Cells)
MsgBox "The last cell found is: " & Last(3, ActiveSheet.Cells)
MsgBox "The last column (A1) found is: " & Last(4, ActiveSheet.Cells)
End Sub
Function Last(choice As Integer, rng As Range)
' 1 = last row
' 2 = last column (R1C1)
' 3 = last cell
' 4 = last column (A1)
Dim lrw As Long
Dim lcol As Integer
Select Case choice
Case 1:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
Case 2:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
lcol = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Last = Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0
Case 4:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Last = R1C1converter("R1C" & Last, 1)
For i = 1 To Len(Last)
s = Mid(Last, i, 1)
If Not s Like "#" Then s1 = s1 & s
Next i
Last = s1
End Select
End Function
Function R1C1converter(Address As String, Optional R1C1_output As Integer, Optional RefCell As Range) As String
'Converts input address to either A1 or R1C1 style reference relative to RefCell
'If R1C1_output is xlR1C1, then result is R1C1 style reference.
'If R1C1_output is xlA1 (or missing), then return A1 style reference.
'If RefCell is missing, then the address is relative to the active cell
'If there is an error in conversion, the function returns the input Address string
Dim x As Variant
If RefCell Is Nothing Then Set RefCell = ActiveCell
If R1C1_output = xlR1C1 Then
x = Application.ConvertFormula(Address, xlA1, xlR1C1, , RefCell) 'Convert A1 to R1C1
Else
x = Application.ConvertFormula(Address, xlR1C1, xlA1, , RefCell) 'Convert R1C1 to A1
End If
If IsError(x) Then
R1C1converter = Address
Else
'If input address is A1 reference and A1 is requested output, then Application.ConvertFormula
'surrounds the address in single quotes.
If Right(x, 1) = "'" Then
R1C1converter = Mid(x, 2, Len(x) - 2)
Else
x = Application.Substitute(x, "$", "")
R1C1converter = x
End If
End If
End Function
7
简单快捷:
Dim lastRow as long
Range("A1").select
lastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
使用例:
cells(lastRow,1)="Ultima Linha, Last Row. Youpi!!!!"
'or
Range("A" & lastRow).Value = "FIM, THE END"
-1
sub test()
msgbox Worksheets("sheet_name").Range("A65536").End(xlUp).Row
end sub
这是因为 “A65536” 列中寻找价值
1
我会喜欢使用添加更可靠的方法找上次使用的一行:
lastRow = Sheet1.UsedRange.Row + Sheet1.UsedRange.Rows.Count - 1
同样找到上次使用的栏,你可以see this
导致立即窗口:
?Sheet1.UsedRange.Row+Sheet1.UsedRange.Rows.Count-1
21
0
Public Function GetLastRow(ByVal SheetName As String) As Integer
Dim sht As Worksheet
Dim FirstUsedRow As Integer 'the first row of UsedRange
Dim UsedRows As Integer ' number of rows used
Set sht = Sheets(SheetName)
''UsedRange.Rows.Count for the empty sheet is 1
UsedRows = sht.UsedRange.Rows.Count
FirstUsedRow = sht.UsedRange.Row
GetLastRow = FirstUsedRow + UsedRows - 1
Set sht = Nothing
End Function
片.UsedRange.Rows。算:所使用的行数retrurn,不包括第一行以上的空行用过
如果第1行是空的,并且最后使用的行是10,UsedRange.Rows.Count将返回图9中,不10.
该函数计算UsedRange的第一行数加上UsedRange行的数量。
相关问题
- 1. Excel电子表格检查哪一行包含数据
- 2. 查找包含文本的最后一个单元格
- 3. Excel宏包含参数在范围
- 4. Excel宏,如果包含其他
- 5. 宏在Excel中删除包含全0
- 6. 如何在包含乳胶的表格中包含长文字?
- 7. 查找包含一些
- 8. Excel VBA:如何将包含数据的最后一列与列之前相乘?
- 9. Excel公式来查找表中包含的值的最右边一列
- 10. 查找和包裹包含换行符
- 11. 包含换行符的一个单元格中的总数 - Excel
- 12. 如何在Excel中执行“包含”
- 13. Excel:找出单元格是否包含/包含单元格范围内的值
- 14. 如何将包含行名的表格从Matlab导出到Excel?
- 15. 查找在excel中包含关键字的单元格VBA
- 16. 如何在数据表中包含tfoot
- 17. 确定Word文档或Excel电子表格是否包含宏
- 18. Perl - 查找包含连字符的最后一个字
- 19. 在postgresql数据库中查找包含非数字值的行
- 20. 查找最频繁包含给定列中最大值的行
- 21. 如何查找包含有关列的数据的第一个单元格?
- 22. excel如何计算包含数据的行数?
- 23. 查找包含jQuery中
- 24. Excel如果单元格中包含“ - ”数字然后移动
- 25. 如何查找包含文件中给定整数的行?
- 26. Google表格 - 查询包含数组
- 27. 使包含表格的一个div分为两个div,其中包含表格
- 28. 如何找到包含数据vba的单元格的最后一行以设置打印区域?
- 29. Excel - 一系列单元格是否包含列表中的值?从列表中包含的任何值: -
- 30. Rails包含表格
可以找到更详细的回复[这里](http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba) – 2013-12-19 14:20:13
而对于这个问题,早期的帖子[这里:)](http://stackoverflow.com/questions/4872512/last-not-empty-cell-in-row-excel-vba/8583926#8583926) – brettdj 2013-12-21 02:56:31
[在VBA中查找上次使用的单元时发生错误] (https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) – Masoud 2017-07-07 14:23:23