Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Ayden Smith   on Sep 30 In MS Office Category.

  
Question Answered By: Sarah Campbell   on Sep 30

Post the following code  into a VBA module. Then click any cell on the worksheet
where you want to add the row  numbers (to make sure it is the active sheet).
From the Tools menu, select Macro >> Macros >>AddRowNbr >> Run.

Sub AddRowNbr()
'Declare variables.
Dim LastRng As String, c As Range
'Find the last cell on the sheet.
LastRng$ = FindLastCell(ActiveSheet)
If LastRng$ = "ERROR" Then
MsgBox "Can't find last cell", , "AddRowNbr"
End If
'Set c to be the last cell.
Set c = Range(LastRng$)
'Go to row 1 in the next column  after c.
Cells(1, c.Column + 1).Activate
'Number all the rows of data.
ActiveCell.Value = 1
ActiveCell.Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, _
Date:=xlDay, Step:=1, Stop:=c.Row, Trend:=False
'Free the object variable.
Set c = Nothing
End Sub

Private Function FindLastCell(Wksht As Worksheet) As String
'Returns address of last cell used (highest row & col)
'on specified sheet.
Dim LastRow As Long
Dim LastCol As Integer
On Error GoTo FLCerr1
With Wksht
LastRow = 0
LastCol = 0
LastRow& = .Cells.Find("*", After:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
LastCol% = .Cells.Find("*", After:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
FindLastCell$ = Cells(LastRow&, LastCol%).AddressLocal
Exit Function
FLCerr1:
'Empty worksheet, or unknown error.
FindLastCell$ = "ERROR"
End Function

Share: 

 

This Question has 11 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Last Row and Column Or get search suggestion and latest updates.


Tagged: