Solved

Excel automatically numbering column

Posted on 2014-10-01
5
72 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
[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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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