Tìm dòng đầu tiên và cuối cùng bằng vba

Làm thế nào để tìm hàng cuối cùngcột cuối cùng hay ô cuối cùng của một bảng tính sử dung VBA.
Bài viết được biên soạn dưới đây sử hướng dẫn làm việc ấy một cách chi tiết và dễ hiểu nhất.


Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column

Dim DongCuoi As Long
Dim CotCuoi As Long
    
    'Find the last non-blank cell in column A(1)
    DongCuoi = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Find the last non-blank cell in row 1
    CotCuoi = Cells(1, Columns.Count).End(xlToLeft).Column
    
    MsgBox "Last Row: " & lRow & vbNewLine & _
            "Last Column: " & lCol
  
End Sub

VBA Last Row (Dòng cuối cùng)

Dòng cuối cùng trong một cột

Để tìm đường dòng chứa dữ liệu cuối cùng trong một cột chúng ta sử dụng thuộc tính End trong VBA Range.
Dim lastRow as Range

 'Lấy dòng cuối cùng có chứa dữ liệu trong cột
Debug.Print Range("A1").End(xlDown).Row 'Result: 5
Set lastRow = Range("A1").End(xlDown).EntireRow

 'Lấy ô cuối cùng có chứa dữ liệu trong cột
Dim lastRow as Range
Set lastRow = Range("A1").End(xlDown)

Dòng chứa dữ liệu cuối cùng trong worksheet

Để tìm dòng cuối cùng có chứa dữ liệu trong worksheet chúng ta cần sử dụng thuộc tính SpecialCells hoặc Find của Excel VBA Range.
Dim lastRow as Range, ws As Worksheet
Set ws = ActiveSheet

'Lấy dòng chứa dữ liệu cuối cùng trong worksheet sử dụng SpecialCells
Debug.Print ws.Cells.SpecialCells(xlCellTypeLastCell).Row
Set lastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).EntireRow

'Lấy dòng chứa dữ liệu cuối cùng trong worksheet sử dụng Find
Debug.Print Debug.Print ws.Cells.Find(What:="*", _
After:=ws.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Set lastRow = Debug.Print ws.Cells.Find(What:="*", _
After:=ws.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).EntireRow

Dòng cuối cùng trong worksheet UsedRange

Để tìm dòng cuối cùng trong worksheet UsedRange chúng ta cần sử dụng thuộc tính UsedRange của VBA worksheet
'Tìm dòng cuối cùng trong Worksheet UsedRange
Dim lastRow as Range, ws As Worksheet
Set ws = ActiveSheet
Debug.Print ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
Set lastRow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).EntireRow

VBA Last Column (Cột cuối cùng)

Cột cuối cùng trong hàng có chứa dữ liệu

Để tìm được cột cuối cùng trong hàng có chứa dữ liệu chúng ta dùng thuộc tính End
Dim lastColumn as Range

'Tìm cột có dữ liệu cuối cùng trong hàng
Debug.Print Range("A1").End(xlRight).Row 'Result: 5
Set lastColumn = Range("A1").End(xlRight).EntireColumn

'Tìm ô cuối cùng có dữ liệu trong cột
Dim lastColumn as Range
Set lastColumn = Range("A1").End(xlRight)

Cột cuối cùng có chứa dữ liệu trong Worksheet.

Để tìm cột cuối cùng có chứa dữ liệu trong worksheet chúng ta cần sử dụng thuộc tính SpecialCells hoặc Find.
Dim lastColumn as Range, ws As Worksheet
Set ws = ActiveSheet

'Tìm cột cuối cùng có chứa dữ liệu trong Worksheet sử dụng SpecialCells
Debug.Print ws.Cells.SpecialCells(xlCellTypeLastCell).Column
Set lastColumn = ws.Cells.SpecialCells(xlCellTypeLastCell).EntireColumn

 'Tìm cột cuối cùng có chứa dữ liệu trong Worksheet sử dụng Find
Debug.Print Debug.Print ws.Cells.Find(What:="*", _
After:=ws.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Set lastColumn = Debug.Print ws.Cells.Find(What:="*", _
After:=ws.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).EntireColumn

Cột cuối cùng trong Worksheet UsedRange

Để tìm cột cuối cùng trong Worksheet UsedRange chúng ta phải sử dụng thuộc tính UsedRange.
'Get Last Column in Worksheet UsedRange
Dim lastColumn as Range, ws As Worksheet
Set ws = ActiveSheet
Debug.Print ws.UsedRange.Columns(ws.UsedRange.Columns.Count).Column
Set lastColumn = ws.UsedRange.Columns(ws.UsedRange.Columns.Count).EntireColumn

VBA Last Cell (Ô cuối cùng)

Ô cuối cùng trong bảng dữ liệu

Để tìm ô cuối cùng trong một bảng dữ liệu (bảng không có ô trống) chúng ta sử dụng thuộc tính End.
Dim lastCell as Range

'Tìm ô cuối cùng trong bảng dữ liệu
SetlastCell = Range("A1").End(xlRight).End(xlDown)
Debug.Print "Row: " & lastCell.row & ", Column: " & lastCell.column

Ô cuối cùng có chứa dữ liệu trong Worksheet.

Để tìm ô có chứa dữ liệu nằm cuối cùng trong Worksheet ta cần sử dụng thuộc tính SpecialCells hoặc Find.
Dim lastCell as Range, ws As Worksheet
Set ws = ActiveSheet

'Tìm ô cuối cùng có chứa dữ liệu trong Worksheet sử dụng SpecialCells
Set lastCell = ws.Cells.SpecialCells(xlCellTypeLastCell)
Debug.Print "Row: " & lastCell.row & ", Column: " & lastCell.column

'Tìm ô cuối cùng có chứa dữ liệu trong Worksheet sử dụng Find
Set lastColumn = Debug.Print ws.Cells.Find(What:="*", _
        After:=ws.Cells(1), _
        Lookat:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False)
Debug.Print "Row: " & lastCell.row & ", Column: " & lastCell.column

Ô cuối cùng trong Worksheet UsedRange

Để tìm ô cuối cùng trong Worksheet UsedRange ta cần sử dụng thuộc tính UsedRange.
'Get Last Cell in Worksheet UsedRange
Dim lastCell as Range, ws As Worksheet
Set ws = ActiveSheet

Set lastCell = ws.UsedRange.Cells(ws.UsedRange.Rows.Count,ws.UsedRange.Columns.Count)
Debug.Print "Row: " & lastCell.row & ", Column: " & lastCell.column

VBA UserRange (Khu vực đã sử dụng)

UsedRange là khu vực được sử dụng và lưu trong Excel. Những khu vực mà đã có nhập liệu dù đã clear thì vẫn thuộc khu vực UsedRange.

Làm thế nào để kiểm tra UsedRange

Cách đơn giản nhất là chọn một ô A1 trong bảng tính Excel và bấm tổ hợp phím Ctrl + Shift + End. UsedRange sẽ được quét chọn.

Check UsedRange trong VBA

Sử dụng code dưới đây để kiểm tra UsedRange
Dim lastCell As Range, firstCell As Range, ws As Worksheet
Set ws = ActiveSheet
    
Set lastCell = ws.UsedRange.Cells(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count)
Set firstCell = ws.UsedRange.Cells(1, 1)
Debug.Print "Ô đầu tiên trong UsedRange. Row: " & firstCell.Row & ", Column: " & firstCell.Column
Debug.Print "Ô cuối cùng trong UsedRange. Row: " & lastCell.Row & ", Column: " & lastCell.Column    
Kết quả sẽ cho ra Ô đầu tiền trong UsedRange; Row: 2, Column: 2 Ô cuối cùng trong UsedRange; Row: 5, Column: 6

Source: http://analystcave.com/excel-vba-last-row-last-column-last-cell/#VBA_UsedRange

Đăng nhận xét (0)
Mới hơn Cũ hơn