Solved

Number each row in Excel 2007 spreadsheet using VBA

Posted on 2015-02-06
5
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

690 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