Solved

Excel automatically numbering column

Posted on 2014-10-01
5
69 Views
Last Modified: 2014-10-13
I have a worksheet where I am manually adding rows in a column. The amount of rows in the column could change each time. On one worksheet it can be 1100 on another worksheet the number can be 5,000.  I would like to collectively group those rows in the coiumn no matter how many in groups of 30 and automatically name them in excel. Currently I am doing the following


groupbatch1
groupbatch1
groupbatch1
....this is repeated 30times
groupbatch2
groupbatch2
groupbatch2
groupbatch2
....this is repeated 30times
groupbatch3
groupbatch3
groupbatch3

I would like to see if this can be automated in excel
0
Comment
Question by:techdrive
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40355149
Yes, using VBA/macros, this could be automated.  Try this subroutine:
Option Explicit
Sub Add_Group()
    Dim strGroupName As String
    Dim lngLastRow As Long
    strGroupName = InputBox("Enter Group Name", "Group Name")
    If strGroupName <> "" Then
       lngLastRow = Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Row
       Range(Cells(lngLastRow, 1), Cells(lngLastRow + 29, 1)) = strGroupName
    End If
End Sub

Open in new window


This adds/appends 30 identical values in column A.  If you need it in another column:
* change the "A" in line 7 and
* change line 8 - from 1 (A) to the number of the column to add (ex., B - 2, C - 3, etc.)

By the way, this only works in groups of 30.  To expand this, I'd need to know the syntax of how you want your groupbatch labels listed.  For example, would it be batchname-n, where n is an incrementing number that you specify.

Regards,
-Glenn
0
 

Author Comment

by:techdrive
ID: 40364614
Glenn thanks for the code. I would like to append after batchname-n the actual date. So if I could get some every 30 lines with batchname-10-5-2014-1 and then 2, then 3 and so on. By the way it places output in cell A however its not changing every 30 lines /.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40364709
Okay, this code will create groups of 30 batchnames with a date and numeric suffix added to differentiate.  The user will be required to enter two values:
*  BatchName
*  Number of groups (used for the suffix value)
Option Explicit
Sub Add_Group()
    Dim strGroupName As String
    Dim lngLastRow As Long
    Dim x, intGroups As Integer
    strGroupName = InputBox("Enter Group Name", "Group Name")
    If strGroupName = "" Then Exit Sub
    intGroups = InputBox("Enter Number of Groups", "Group Quantity")
    If intGroups < 1 Then Exit Sub
    For x = 1 To intGroups
        If strGroupName <> "" Then
           lngLastRow = Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Row
           Range(Cells(lngLastRow, 1), Cells(lngLastRow + 29, 1)) = _
               strGroupName & "-" & Format(Now(), "m-d-yyyy") & "-" & x
        End If
    Next x
End Sub

Open in new window


Again, it places it in column A, starting at row 2.

Regards,
-Glenn
0
 

Author Comment

by:techdrive
ID: 40365832
awesome thanks it works
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40365944
You're welcome.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

786 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