Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Excel/VBA Automatic Numbering

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
manpreet.khera@gmail.com - 07 Feb 2007 15:11 GMT
Hey,

I am trying to use a macro to get my excel worksheet to number
automatically (the rows)....however when i insert a row, the number
tracking is off and there is no automatic numbering...

This is the code I am using:

Sub Autonumber()
done = 0
X = 2
   Do While done < 1
       If (Cells(X, 1) = "") Then
           entry = X - 1
           done = 1
       Else
           X = X + 1
       End If
   Loop

Cells(X, 1) = entry
X = entry + 1

End Sub

Can anyone please help me ASAP!!!

THANKS :)

Munny Khera
JLatham - 08 Feb 2007 22:41 GMT
Try putting this code into the Worksheet's code area:

Private Sub Worksheet_Change(ByVal Target As Range)

   If Target.Rows.Count = 1 And _
       Target.Address = "$" & Target.Row & ":$" & Target.Row Then
       Range("A" & Target.Row).Formula = "=Row()-1"
   End If

End Sub

To get to the proper place to put the code, right-click on the worksheet's
tab and choose [View Code] from the list.  Copy the above code and paste it
in.  Repeat for all sheets you need this function on.

What it does - it tests to see if you've inserted a row by first asking if
the count of rows in the current selection (Target) is one AND if that is
true, by seeing if the address returned for the Target looks like a row
address,  $6:$6 for example if you'd just inserted a new row 6.

The formula =ROW()-1
returns the row number -1, so at row 6 it would display 5.  I presume that's
what you wanted based on what I saw in your code.

Hope this helps some.

> Hey,
>
[quoted text clipped - 26 lines]
>
> Munny Khera
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.