Solved

Number each row in Excel 2007 spreadsheet using VBA

Posted on 2015-02-06
5
67 Views
Last Modified: 2016-02-10
Hi all.

I have an Excel template where the end user enters a "po number" the template then brings in the records for that po number starting in cell B8 and across to J8. Sometimes there may be 3 records pulled, other times 50, there is no way of knowing how many records are brought in.

I want to be able to number each row (starting with cell A8) that is brought in. For example, if 3 records are brought in, then I would like to put 1, 2, 3 in column A. If there are 50 records, then insert 1 - 50 in column A.

How can I do this using VBA?

Thank you in advance.
0
Comment
Question by:printmedia
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40594075
Range("A8").Activate
    ActiveCell.FormulaR1C1 = "=ROW()-7"
    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
    Range("A8").Select
    Selection.AutoFill Destination:=Range("A8", Cells(LastRow, 1))

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 40594097
Thanks!
0
 

Author Comment

by:printmedia
ID: 40594112
Would you mind explaining what each line of code does so I can understand it fully?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40594132
'Activate Cell A8
Range("A8").Activate

'Set the formula to Row() - 7  Starting @Number 1 in Cell A8
    ActiveCell.FormulaR1C1 = "=ROW()-7"

'Find the last row based on Column B that has data
    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row

'Go Back to A8
    Range("A8").Select

'AutoFill the formula, from A8 to A<LastRow>
    Selection.AutoFill Destination:=Range("A8", Cells(LastRow, 1))
0
 

Author Comment

by:printmedia
ID: 40594139
Ah makes sense now, I was a little confused with the ActiveCell.FormulaR1C1. Thanks again it works great.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now