2008-09-16 130 views
50

如何查找包含特定列和特定工作表中数据的最后一行?如何查找包含Excel表格中包含宏数据的最后一行?

+4

可以找到更详细的回复[这里](http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba) – 2013-12-19 14:20:13

+1

而对于这个问题,早期的帖子[这里:)](http://stackoverflow.com/questions/4872512/last-not-empty-cell-in-row-excel-vba/8583926#8583926) – brettdj 2013-12-21 02:56:31

+0

[在VBA中查找上次使用的单元时发生错误] (https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) – Masoud 2017-07-07 14:23:23

回答

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

Isempty(r.value)是否真的需要检查?它不应该总是有价值吗? – gdelfino 2015-03-26 14:33:54

+0

@gdelfino是的。例如。列A在行1-10中具有值,列B在行1-8中具有值。 `UsedRange`将是`A1:B10`,与`B:B`的交点将是`B1:B10`,最后一个单元是`B10`并且它是空的。 – GSerg 2015-03-26 14:57:39

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

enter image description here

导致立即窗口:

?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行的数量。

相关问题