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: Jawahir Burki   on Sep 30

The following allows you to run a macro which will present an input box.
When the input box is present you may select the last column  with your
mouse.

Sub NumberRows()
Dim rRange As Range
Dim sPrompt, sTitle As String
Dim lRangeCount As Long
Dim lColumn As Long
Dim sRange As String
Dim l As Long

'Makes Input Box setup easier to read
sPrompt = "Enter some text for your input box" 'This may not be
necessary but it looks nice
sTitle = "A title for input box" 'Again not necessary but it looks good
On Error Resume Next
Set rRange = Application.InputBox(Prompt:=sPrompt, Title:=sTitle, _
Default:=ActiveCell.Address, Type:=8)
If Err.Number = 424 Then 'In case user hits the cancel button.
Resume 0
Exit Sub
End If
Resume 0 'Always resume 0 whenever you have an on error resume next
statement

'The following was the only way I could find to properly select and
identify the starting active cell based on the input box selection.

sRange = rRange.Address
Range(sRange).Select
Set rRange = Application.ActiveCell 'Identifies starting row  and column
sRange = rRange.Address
Range(sRange).Select
ActiveCell.Offset(0, 1).Select
ActiveCell.EntireColumn.Insert Shift:=xlToRight 'Inserts a column to the
right of your selected data.

lColumn = rRange.Column
lRangeCount = Cells(Rows.Count, lColumn).End(xlUp).Row 'As Dawn
mentioned this line of code  is only efficient when there are no blank
cells between the start and end  of the selected range.

'This will number  the rows for you.
For l = 1 To lRangeCount 'Start at 1 to skip header row, use 0 to start
at first row
ActiveCell.Offset(l, 0).Value = l + 1
Next

End Sub

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: