Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Number each row in Excel 2007 spreadsheet using VBA

Posted on 2015-02-06
5
Medium Priority
?
74 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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 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 41

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

636 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