我怎样才能find最后一行,包含Excel表格中的数据与macros?
如何find包含特定列和特定工作表中数据的最后一行?
怎么样:
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
即使最后一行中只有一个单元格有数据,也会返回最后一个单元格的行号。
您应该使用.End(xlup)
而不是使用65536您可能想要使用:
sheetvar.Rows.Count
这样它适用于Excel 2007,我相信有超过65536行
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")
简单快捷:
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"
这是find最后一行,最后一列或最后一个单元格的解决scheme。 它解决了find的列的A1 R1C1参考风格困境。 希望我可以信任,但不能find/记得我从哪里得到它,所以“谢谢!” 不pipe是那个地方在哪里发布原始代码。
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
Public Function LastData(rCol As Range) As Range Set LastData = rCol.Find("*", rCol.Cells(1), , , , xlPrevious) End Function
用法: ?lastdata(activecell.EntireColumn).Address
我想使用UsedRange
添加一个更可靠的方法来查找最后使用的行:
lastRow = Sheet1.UsedRange.Row + Sheet1.UsedRange.Rows.Count - 1
同样的,find最后使用的列,你可以看到这个
在立即窗口中的结果:
?Sheet1.UsedRange.Row+Sheet1.UsedRange.Rows.Count-1 21
第一个函数将光标移动到列中最后一个非空行。 第二个函数打印该列的行。
Selection.End(xlDown).Select MsgBox (ActiveCell.Row)
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
sheet.UsedRange.Rows.Count:重新使用的行数,不包括使用的第一行之上的空行
如果第1行是空的,最后一个使用的行是10,则UsedRange.Rows.Count将返回9,而不是10。
该函数计算UsedRange的第一行数加上UsedRange行数。
sub test() msgbox Worksheets("sheet_name").Range("A65536").End(xlUp).Row end sub
这是因为“A65536”
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