Làm cách nào để tìm hàng cuối cùng chứa dữ liệu trong một cột cụ thể và trên một trang tính cụ thể?Làm cách nào để tìm hàng cuối cùng chứa dữ liệu trong trang tính Excel bằng macro?
Trả lời
Làm thế nào về:
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
Re Comment
này
Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
Sẽ trả về số hàng của ô cuối cùng ngay cả khi chỉ một ô duy nhất trong hàng cuối cùng có dữ liệu.
Chức năng này sẽ trả lại kết quả sai khi: - strColumn là một số - có một số dữ liệu trong hàng 65536 - bạn đang sử dụng Excel 2007 với hơn 65.536 hàng – GSerg
@GSerg Bạn có thể sửa mã, sau đó xóa các bình luận nếu bạn thích (vì nó sẽ không áp dụng nữa
Tôi tin rằng điều này sẽ báo cáo sai ô nào là ô cuối cùng nếu ô có dữ liệu và sau đó dữ liệu đã bị xóa. –
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
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
Cách sử dụng:
? LastRowIndex(ActiveSheet, 5)
? LastRowIndex(ActiveSheet, "AI")
Việc kiểm tra isempty (r.value) có thực sự cần thiết không? Không phải nó luôn luôn có một giá trị? – gdelfino
@gdelfino Đúng vậy. Ví dụ. cột A có các giá trị trong hàng 1-10, cột B có các giá trị trong hàng 1-8. 'UsedRange' sẽ là' A1: B10', giao điểm với 'B: B' sẽ là' B1: B10', ô cuối cùng là 'B10' và ô trống. – GSerg
Hàm đầu tiên di chuyển con trỏ đến hàng không trống cuối cùng trong cột. Chức năng in thứ hai là hàng cột.
Selection.End(xlDown).Select
MsgBox (ActiveCell.Row)
Điều này không hoạt động nếu có bất kỳ ô trống nào trong cột. –
Bạn nên sử dụng .End(xlup)
nhưng thay vì sử dụng 65536 bạn có thể muốn sử dụng:
sheetvar.Rows.Count
Bằng cách đó nó hoạt động cho Excel 2007 mà tôi tin rằng có hơn 65.536 hàng
Public Function LastData(rCol As Range) As Range
Set LastData = rCol.Find("*", rCol.Cells(1), , , , xlPrevious)
End Function
Cách sử dụng: ?lastdata(activecell.EntireColumn).Address
Đây là giải pháp để tìm hàng cuối cùng, l cột ast hoặc ô cuối cùng. Nó giải quyết tình trạng tiến thoái lưỡng nan kiểu tham chiếu A1 R1C1 cho cột mà nó tìm thấy. Ước gì tôi có thể cho tín dụng, nhưng không thể tìm thấy/nhớ nơi tôi đã nhận nó, vì vậy "Cảm ơn!" với bất cứ ai nó đã đăng mã gốc ở đâu đó ra khỏi đó.
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
đơn giản và nhanh chóng:
Dim lastRow as long
Range("A1").select
lastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
sử dụng Ví dụ:
cells(lastRow,1)="Ultima Linha, Last Row. Youpi!!!!"
'or
Range("A" & lastRow).Value = "FIM, THE END"
Hoặc như này 'chức năng getSheetLastRow (sheet2Check như bảng) lastRow = sheet2Check .Cells.Find ("*", SearchOrder: = xlByRows, SearchDirection: = xlPrevious) .Row getSheetLastRow = lastRow cuối chức năng' – user2988717
sub test()
msgbox Worksheets("sheet_name").Range("A65536").End(xlUp).Row
end sub
Đây là tìm kiếm giá trị trong cột A vì "A65536"
tôi sẽ muốn thêm một cách đáng tin cậy hơn bằng cách sử dụng UsedRange
để tìm hàng đã qua sử dụng lần cuối:
lastRow = Sheet1.UsedRange.Row + Sheet1.UsedRange.Rows.Count - 1
Tương tự như vậy để tìm các cột được sử dụng cuối cùng bạn có thể see this
quả trong Immediate Window:
?Sheet1.UsedRange.Row+Sheet1.UsedRange.Rows.Count-1
21
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
tờ .UsedRange.Rows.Count: số retrurn hàng sử dụng, không bao gồm hàng trống trên hàng đầu tiên được sử dụng
nếu dòng 1 là trống rỗng, và đã qua sử dụng hàng cuối cùng là 10, UsedRange.Rows.Count sẽ trở lại 9, chứ không phải 10
Hàm này tính số hàng đầu tiên của UsedRange cộng với số hàng UsedRange.
Có thể tìm thấy câu trả lời chi tiết hơn [TẠI ĐÂY] (http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba) –
Và về vấn đề đó, bài đăng trước đó [ở đây :)] (http://stackoverflow.com/questions/4872512/last-not-empty-cell-in-row-excel-vba/8583926#8583926) – brettdj
Có thể trùng lặp của [Lỗi khi tìm ô được sử dụng lần cuối trong VBA] (https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) – Masoud