Vous êtes sur la page 1sur 4

The macro's give you the row or column number of the last cell with data in one row

or one column. Note : This code below will not work correct if the last row with data is hidden . Sub LastRowInOneColumn() 'Find the last used row in a Column: column A in this example Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With MsgBox LastRow End Sub Sub LastColumnInOneRow() 'Find the last used column in a Row: row 1 in this example Dim LastCol As Integer With ActiveSheet LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column End With MsgBox LastCol End Sub

Last Row : Sub xlCellTypeLastCell_Example_Row() Dim LastRow As Long With ActiveSheet LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row End With MsgBox LastRow End Sub Sub UsedRange_Example_Row() Dim LastRow As Long With ActiveSheet.UsedRange LastRow = .Rows(.Rows.Count).Row End With MsgBox LastRow End Sub Last Column : Sub xlCellTypeLastCell_Example_Column() Dim LastColumn As Long With ActiveSheet LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column End With MsgBox LastColumn End Sub Sub UsedRange_Example_Column()

Dim LastColumn As Long With ActiveSheet.UsedRange LastColumn = .Columns(.Columns.Count).Column End With MsgBox LastColumn End Sub Use the "Last" function to Find the last row, column or cell in range or worksheet In the example macros we call the function Last and this function have two argum ents Argument 1 = last 2 = last 3 = last Argument 1 can be 1, 2 or 3 row column cell 2 is the range where you want to search in

Copy the example macros together with the function "Last" in a normal module of your workbook. Sub LastRow_Example() Dim LastRow As Long Dim rng As Range ' Use all cells on the sheet Set rng = Sheets("Sheet1").Cells 'Use a range on the sheet 'Set rng = Sheets("Sheet1").Range("A1:D30") ' Find the last row LastRow = Last(1, rng) ' After the last row with data change the value of the cell in Column A rng.Parent.Cells(LastRow + 1, 1).Value = "Hi there" End Sub Sub LastColumn_Example() Dim LastCol As Long Dim rng As Range ' Use all cells on the sheet Set rng = Sheets("Sheet1").Cells 'Or use a range on the sheet 'Set rng = Sheets("Sheet1").Range("A1:D30") ' Find the last column LastCol = Last(2, rng) ' After the last column with data change the value of the cell in row 1 rng.Parent.Cells(1, LastCol + 1).Value = "Hi there" End Sub

Sub LastCell_Example() Dim LastCell As String Dim rng As Range ' Use all cells on the sheet Set rng = Sheets("Sheet1").Cells 'Or use a range on the sheet 'Set rng = Sheets("Sheet1").Range("A1:D30") ' Find the last cell LastCell = Last(3, rng) ' Select from A1 till the last cell in Rng With rng.Parent .Select .Range("A1", LastCell).Select End With End Sub 'This is the function we use in the macro's above Function Last(choice As Long, rng As Range) 'Ron de Bruin, 5 May 2008 ' 1 = last row ' 2 = last column ' 3 = last cell Dim lrw As Long Dim lcol As Long 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 On Error GoTo 0 On Error Resume Next lcol = rng.Find(What:="*", _ After:=rng.Cells(1), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 On Error Resume Next Last = rng.Parent.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 End Select End Function

Vous aimerez peut-être aussi