Solved

Excel automatically numbering column

Posted on 2014-10-01
5
66 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
awesome thanks it works
0
 
LVL 27

Expert Comment

by:Glenn Ray
Comment Utility
You're welcome.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 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

8 Experts available now in Live!

Get 1:1 Help Now