+ Reply to Thread
Results 1 to 2 of 2

Modify Unique Identifier Code to Place Values in Different Column

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2020
    Location
    British Columbia, Canada
    MS-Off Ver
    2015
    Posts
    12

    Modify Unique Identifier Code to Place Values in Different Column

    Hello all,

    I am an absolute noob when it come to VBA and mostly I am running other's code without fully knowing what the lines are doing, so please forgive me if I ask something that isn't possible or seems obvious.
    Thank you so much in advance for your help!

    Overall picture:

    I am working on a macro to:
    1. take a series of records from one sheet and copy them to another (currently manually done - will work on this later)
    2. Replicate those records into multiple copies of the same information based on the value in column "M"
    3. Add a unique identifier to the now duplicated values so that each record is now unique

    So far I have step two completed with the following code:

    Step 2a)
    Sub Add_Records_by_cell_value()
    
    Dim wks As Worksheet
    Dim iRow As Long
    Dim firstrow As Long
    Dim lastrow As Long
    Dim HowManyMore As Long
    
    Set wks = Worksheets("Sheet2")
    With wks
    firstrow = 2 'headers in row 1???
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    For iRow = lastrow To firstrow Step -1
    HowManyMore = .Cells(iRow, "M").Value - 1
    If HowManyMore > 0 Then
    .Rows(iRow + 1).Resize(HowManyMore).Insert
    .Cells(iRow + 1, "A").Resize(HowManyMore, 1).Value _
    = .Cells(iRow, "A").Value
    .Cells(iRow + 1, "M").Resize(HowManyMore, 1).Value _
    = .Cells(iRow, "M").Value
    End If
    Next iRow
    
    End With
    End Sub
    Step 2b)
    Sub Replicate_data()
    ' step to populate cells with previous info
    
        Range("A1:L1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Range("C3").Activate
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Application.CutCopyMode = False
        Selection.FormulaR1C1 = "=R[-1]C"
    End Sub
    I then found a code (below) to add a unique identifier to the repeated values but I want to have these values placed in column "O" rather than right beside the 1st column ("A"). It would also be nice to maintain the leading zeros from the Lot # but I know that can be tricky and it is not essential.

    Step 3)
    Sub Numbering2()
        Dim rng As Range
        Dim rngcnt As Range
        Dim firstrow As Long
        Dim lastrow As Long
        Dim columnNumber As Long
        Dim wks As Worksheet
    
        Set wks = Worksheets("Sheet2") 'change to your sheet
        firstrow = 2 'change to your first row of data
        columnNumber = 1 'change to the column number
    
        With wks
            lastrow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
            For i = firstrow To lastrow
                .Cells(i, columnNumber + 1) = .Cells(i, columnNumber) & "_" & Format(Application.WorksheetFunction.CountIf(.Range(.Cells(firstrow, columnNumber), .Cells(i, columnNumber)), .Cells(i, columnNumber)))
            Next i
        End With
    
    End Sub
    I have attached an excel book with a sheet showing the result after step 1 and 2 using the above code (1st sheet) and the result after running step 3 code (2nd sheet)

    P.S. if anyone has any hints on glaring inefficiencies or repeated/superfluous statements, please don't hesitate to make a suggestion, I really have no idea what I'm doing
    Attached Files Attached Files
    Last edited by AliGW; 02-20-2020 at 07:49 PM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,780

    Re: Modify Unique Identifier Code to Place Values in Different Column

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. As you are new I have done it for you this time.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ 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] sorting rows with unique identifier and then duplicate identifier
    By Roshan.Shakya in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-13-2019, 06:02 AM
  2. Replies: 1
    Last Post: 02-16-2017, 07:53 PM
  3. SQL code sum by unique identifier in column only show max result
    By Jack7774 in forum Access Tables & Databases
    Replies: 0
    Last Post: 08-26-2016, 04:11 PM
  4. Replies: 1
    Last Post: 07-08-2015, 10:07 PM
  5. Replies: 1
    Last Post: 01-29-2015, 04:08 PM
  6. [SOLVED] Copying the details of a code based on their unique identifier.
    By Jarvin24 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2013, 07:32 AM
  7. [SOLVED] add increment identifier to unique values
    By hluk in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-19-2013, 04:44 PM

Tags for this Thread

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