+ Reply to Thread
Results 1 to 11 of 11

Code to create numbers in sequence

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    5

    Code to create numbers in sequence

    To make the querry more simple I have attached the sheet
    A request to kindly access the sheet.

    Thanks
    GKT
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: Code to create numbers in sequence

    Try following code (create a button in jour sheet1 and assign this macro):
    Sub test()
    Dim i As Long, j As Long, current As Long, cell As Range, prefix As String
    Application.ScreenUpdating = False
    For Each cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
      current = Val(IIf(InStr(cell, "-"), Right(cell, Len(cell) - InStrRev(cell, "-")), cell))
      prefix = Replace(cell, current, "")
      Sheets("Sheet2").Cells(j + 1, 2) = cell.Offset(0, 1)
      For i = 0 To cell.Offset(0, 1)
        j = j + 1
        Sheets("Sheet2").Cells(j, 1) = IIf(Len(prefix), prefix & current, current)
        current = current + 1
      Next i
    Next cell
    End Sub
    Best Regards,

    Kaper

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Code to create numbers in sequence

    Try the attached.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create numbers in sequence

    Thanks for the reply Kaper & Jindon.
    The code is perfect but as I wanted it to be executed in the
    same sheet i.e sheet1. Is it possible?
    Else it is perfect.
    Also Jindon I wanted the result in column A only.

    Thanks again
    GKT

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Code to create numbers in sequence

    Even simpler.
    Attached Files Attached Files

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: Code to create numbers in sequence

    Also mine corrected:
    Sub test()
    Dim i As Long, j As Long, k As Long, current As Long, cell As Range, prefix As String
    Application.ScreenUpdating = False
    For k = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
      Set cell = Cells(k, 1)
      current = Val(IIf(InStr(cell, "-"), Right(cell, Len(cell) - InStrRev(cell, "-")), cell))
      prefix = Replace(cell, current, "")
      j = Val(cell.Offset(0, 1))
      If j > 0 Then
        cell.Offset(1, 0).Resize(j, 1).EntireRow.Insert
        For i = 1 To j
          current = current + 1
          Cells(k + j, 1) = IIf(Len(prefix), prefix & current, current)
        Next i
      End If
    Next k
    End Sub
    Slower one, but could be (who knows ) a bit easier to understand
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create numbers in sequence

    Hello again Jindon
    I just re-checked the first code. It is displaying the data in column A.
    Comparing both of your code the I would prefer the first since the data
    is put up in column A . I will manage the data in sheet2.
    In that case even the Kaper solution is workable.
    Only one change if I might require in the future. The data which is
    put up in sheet2 as per the first code. I want the data from A2 onwards considering
    row 1 will be headers.

    Thanks
    GKT

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: Code to create numbers in sequence

    row 1 with headers:
    For k = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Code to create numbers in sequence

    Change to
    Sub test()
        Dim a, b, i As Long, ii As Long, n As Long, x
        With Sheets("sheet1").Cells(1).CurrentRegion
            a = .Value
            ReDim b(1 To UBound(a, 1) * (Application.Sum(.Columns(2)) + UBound(a, 1)), 1 To 2)
        End With
        For i = 1 To UBound(a, 1)
            n = n + 1: b(n, 1) = a(i, 1): b(n, 2) = a(i, 2)
            x = mySplit(a(i, 1))
            For ii = 1 To a(i, 2)
                n = n + 1: b(n, 1) = x(0) & x(1) + ii
            Next
        Next
        With Sheets("sheet2").Cells(2, 1).Resize(n)
            .CurrentRegion.Offset(1).ClearContents
            .Value = b
            .Parent.Activate
        End With
    End Sub
    
    Function mySplit(ByVal txt As String)
        With CreateObject("VBScript.RegExp")
            .Pattern = "^(\D*)(\d+)$"
            mySplit = Split(.Replace(txt, "$1;;$2"), ";;")
        End With
    End Function
    You can still see col.B, if you change to
    With Sheets("sheet2").Cells(2, 1).Resize(n,2)

  10. #10
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create numbers in sequence

    This is a confusing decision since both the options are acceptable to me
    and can be given SOLVED.
    But since Kaper gave the exact requirement I would grade him first and then to Jindon.
    Also I will use both the codes as and when required.

    However many thanks to both of them.

    GKT

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Code to create numbers in sequence

    Don't worry.

    Your question and the presentation of the result is confusing.
    My result at the first time is exactly the same as your presentation anyway.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Create 19rows Add numbers in sequence excel/access2007
    By accessobsess in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-10-2013, 12:26 PM
  2. [SOLVED] Macro to create new sheets with numbers in sequence
    By canyflyer in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-21-2013, 07:11 PM
  3. [SOLVED] Creating a sequence based on numbers in one column and adding "01,02..." to a new sequence
    By JCR1968 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-05-2013, 08:06 AM
  4. To generate numbers in sequence using vba code
    By Sriram Anand in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-01-2012, 12:35 PM
  5. Replies: 6
    Last Post: 08-02-2006, 11:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1