Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I'm trying to learn more about macros, so any help is appreciated.
I frequently compile a multi-column list of things, then sequentially number the rows after I've made any additions. I've got a header that takes up 9 rows, so the numbering starts in A10.
Column B in all of this is sorted by last name, so whenever new information comes in I frequently have to insert a new to maintain the alphabetical ordering of Column B.
After each insertion or deletion, I have to manually go to A10 and type in "1" and then go to A11 and type in =A10+1, and then drag that down to auto-copy the formula, creating the sequenced list.
This is probably uber-simple, but I can't auto record a macro to make this happen.

A better formula to use is =ROW()-9. If you place this in A10, it will return 1 since 10-9+1. Place it A11, it will return 2 since 11-9=2.
You can then insert rows where ever you want and then drag up or down just enough to fill in the blank numbers.
If you really want a macro...
This code will find the last cell in Column B that contains data and then fill A10:A-whatever with =ROW()-9
Note: The linewrap in this forum makes code look wierd. Paste it into a VBA window and you should get 2 lines of code between Sub...() and End Sub.
Sub AddRowNumbers()
LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("A10:A" & LastRow).Formula = "=ROW()-9"
End Sub

You could also use a page header rather than taking up the first nine rows of data. That way, your data would always start in cell A1.
Life's more painless for the brainless.

While technically correct, a nine line page header is a pain to edit and all page headers are limited as far as how much formatting can be done.
In addition, I'm going to take a guess here and assume the header that the OP mentioned is not a true header, in that it is not repeated on every page.

Thanks guys.
Yeah - not really a "header" that appears on every sheet, just a quick "summary" of the assumptions going into the columns [e.g., multiply what by what] and a summary of the end results [e.g., totals, maxs/mins, counts, etc.].
I wasn't aware of the ROW formula. Nice to know.
Thanks for the VB code too! I'm really most interested in the logic and the how-to behind macros to do a lot of things, so seeing it (much like finding out about ROW) is helpful to see.
Thanks again,
Hoop

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |