Duplicate rows based on call value and and extra column

arendberg

New Member
Joined
Jan 23, 2024
Messages
9
Office Version
  1. 2021
Platform
  1. MacOS
Hey Guys,

Would love some help with the below sheets. I want to duplicate the rows in the Input sheet based on the number of adults and childeren, columns C and D. For every duplicated row there needs to be an extra column showing "Adult" for every adult and "Child" for every child.

Input example
Example.xlsx
ABCDEFGH
1arrivaldeparturechildrenadultstravel_purposenationalitydate_of_birthcountry
21/21/241/26/2402private_tripCH7/12/90CH
31/22/241/26/2424private_tripDE5/9/79CH
41/22/241/27/2401private_tripCH11/2/90CH
51/22/242/3/2413private_tripCH2/7/54CH
61/22/241/24/2401private_tripCH3/20/94CH
Input


Output example
Example.xlsx
ABCDEFGHI
1arrivaldeparturechildrenadultstravel_purposenationalitydate_of_birthcountryClass
21/21/241/26/2402private_tripCH7/12/90CHAdult
31/21/241/26/2402private_tripCH7/12/90CHAdult
41/22/241/26/2424private_tripDE5/9/79CHAdult
51/22/241/26/2424private_tripDE5/9/79CHAdult
61/22/241/26/2424private_tripDE5/9/79CHAdult
71/22/241/26/2424private_tripDE5/9/79CHAdult
81/22/241/26/2424private_tripDE5/9/79CHChild
91/22/241/26/2424private_tripDE5/9/79CHChild
101/22/241/27/2401private_tripCH11/2/90CHAdult
111/22/242/3/2413private_tripCH2/7/54CHAdult
121/22/242/3/2413private_tripCH2/7/54CHAdult
131/22/242/3/2413private_tripCH2/7/54CHAdult
141/22/242/3/2413private_tripCH2/7/54CHChild
151/22/241/24/2401private_tripCH3/20/94CHAdult
Output
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Not sure what you mean? The original data I am using is the whole sheet so not a couple of columns.
The results in the output sheet can be in any form that works. After I have the results I will map them in the correct order on another sheet.
 
Upvote 0
OK, see if this works for you

VBA Code:
Public Sub ExtendRows()
    Dim ws As Worksheet
    Dim i As Long, x As Long, recCnt As Long
    Dim rng As Range, destRng As Range, cell As Range
    Dim adultCnt As Integer, childCnt As Integer

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet11") ' sheet
    Set rng = ws.Range("A1").CurrentRegion
    Set cell = rng.Cells(1)
    Set destRng = ws.Range("M1")
    
    destRng.CurrentRegion.ClearContents
    
    For i = 1 To rng.Rows.Count
        adultCnt = cell.Offset(i, 3).Value
        childCnt = cell.Offset(i, 2).Value
             
        If adultCnt > 0 Then
            For x = 1 To adultCnt
                WriteRecord destRng, rng, recCnt, i, "Adult"
                recCnt = recCnt + 1
            Next
        End If
        
        If childCnt > 0 Then
            For x = 1 To childCnt
                WriteRecord destRng, rng, recCnt, i, "Child"
                recCnt = recCnt + 1
            Next
        End If
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    Set ws = Nothing
End Sub

Private Sub WriteRecord(destRng As Range, rng As Range, recCnt As Long, rowNum As Long, theClass As String)
    destRng.Offset(recCnt, 0) = rng.Offset(rowNum, 0).Value
    destRng.Offset(recCnt, 1) = rng.Offset(rowNum, 1).Value
    destRng.Offset(recCnt, 2) = rng.Offset(rowNum, 2).Value
    destRng.Offset(recCnt, 3) = rng.Offset(rowNum, 3).Value
    destRng.Offset(recCnt, 4) = rng.Offset(rowNum, 4).Value
    destRng.Offset(recCnt, 5) = rng.Offset(rowNum, 5).Value
    destRng.Offset(recCnt, 6) = rng.Offset(rowNum, 6).Value
    destRng.Offset(recCnt, 7) = rng.Offset(rowNum, 7).Value
    destRng.Offset(recCnt, 8) = rng.Offset(rowNum, 8).Value
    destRng.Offset(recCnt, 9) = theClass
End Sub
 
Upvote 0
Wow amazing, almost where I want to be!
Many thanks.

Only issue is that it puts the result in the same sheet and I need it in a new sheet? And if I need to use it on a sheet that has for instance 20 columns I just increase these I guess?
destRng.Offset(recCnt, 8) = rng.Offset(rowNum, 8).Value - So I would just add multiple lines with higher numbers as long as I keep theClass line as the last one?
 
Upvote 0
Correct, you can add more columns by extending the write record function.

This will allow you to copy data to another sheet, just set it where you want.
:)

VBA Code:
Public Sub ExtendRows()
    Dim ws As Worksheet, wsDest As Worksheet
    Dim i As Long, x As Long, recCnt As Long
    Dim rng As Range, destRng As Range, cell As Range
    Dim adultCnt As Integer, childCnt As Integer

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet11") ' sheet
    Set wsDest = ThisWorkbook.Sheets("Sheet12") ' your other sheet
    Set rng = ws.Range("A1").CurrentRegion
    Set cell = rng.Cells(1)
    Set destRng = wsDest.Range("A1") ' change where you want the data
    
    ' if you want to keep headers this needs to change a bit
    destRng.CurrentRegion.ClearContents
    
    For i = 1 To rng.Rows.Count
        adultCnt = cell.Offset(i, 3).Value
        childCnt = cell.Offset(i, 2).Value
             
        If adultCnt > 0 Then
            For x = 1 To adultCnt
                WriteRecord destRng, rng, recCnt, i, "Adult"
                recCnt = recCnt + 1
            Next
        End If
        
        If childCnt > 0 Then
            For x = 1 To childCnt
                WriteRecord destRng, rng, recCnt, i, "Child"
                recCnt = recCnt + 1
            Next
        End If
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    Set ws = Nothing
    Set wsDest = Nothing
End Sub
 
Upvote 0
Solution
How many other columns do you have?
Rows in the dataset?

If the macro is slow. We can change it to either an Array or write a row at a time instead of cell by cell.
 
Upvote 0
This error
 

Attachments

  • Screenshot 2024-01-23 at 17.54.08.png
    Screenshot 2024-01-23 at 17.54.08.png
    153.2 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top