SOLVED

Macro/VBA code to duplicate every N-th row, N times

%3CLINGO-SUB%20id%3D%22lingo-sub-1826583%22%20slang%3D%22en-US%22%3EMacro%2FVBA%20code%20to%20duplicate%20every%20N-th%20row%2C%20N%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1826583%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20a%20newbie%20to%20VBA%20and%20macro%20so%20I%20need%20help%20with%20a%20script%2Fcode%20that%20can%20duplicate%20every%2017th%20row%2C%20four%20number%20of%20times.%20This%20should%20be%20a%20loop.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20attached%20a%20part%20of%20my%20dataset.%20I%20want%20to%20duplicate%20every%2017th%20row%20because%20I%20have%20a%20new%20'Id'%20after%20every%2017th%20row.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20a%20bit%20urgent%20so%20I'm%20really%20trying%20to%20automate%20this%20process.%20Thanks%20for%20any%20help%20you%20can%20provide!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1826583%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1826645%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%2FVBA%20code%20to%20duplicate%20every%20N-th%20row%2C%20N%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1826645%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F845723%22%20target%3D%22_blank%22%3E%40ushbar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20macro%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20InsertRows()%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20m%20%3D%20Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20m%20%3D%20((m%20-%201)%20%5C%2017)%20*%2017%20%2B%201%0A%20%20%20%20For%20r%20%3D%20m%20To%2018%20Step%20-17%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r).EntireRow.Copy%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r%20%2B%201).Resize(4).EntireRow.Insert%0A%20%20%20%20Next%20r%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1827108%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%2FVBA%20code%20to%20duplicate%20every%20N-th%20row%2C%20N%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1827108%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20a%20lot%20for%20your%20quick%20response!%20That%20worked!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20actually%20want%20to%20replace%20the%20four%20new%20rows%20with%20the%20following%20values%20in%20the%20%22Allergener%22%20column%3A%3C%2FP%3E%3CTABLE%20border%3D%220%22%20width%3D%2264%22%20cellspacing%3D%220%22%20cellpadding%3D%220%22%3E%3CTBODY%3E%3CTR%3E%3CTD%20width%3D%2264%22%20height%3D%2220%22%3E%3CSPAN%3EParan%C3%B8tter%3C%2FSPAN%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20height%3D%2220%22%3E%3CSPAN%3EPekann%C3%B8tter%3C%2FSPAN%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20height%3D%2220%22%3E%3CSPAN%3EPistasjn%C3%B8tter%3C%2FSPAN%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20height%3D%2220%22%3E%3CSPAN%3EValn%C3%B8tter%3C%2FSPAN%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20column%20L-O%20should%20be%20empty%20for%20the%20four%20new%20columns.%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20should%20again%20be%20a%20loop%20similar%20to%20the%20original%20post.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EDo%20you%20have%20any%20code%20for%20this%3F%20%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20duplicated%20rows%20from%20the%20code%20above%20do%20not%20always%20have%20the%20same%20values%20because%20the%2017th%20row%20sometimes%20have%20a%20different%20value%20throughout%20the%20dataset.%20That's%20why%20a%20code%20would%20be%20more%20efficient%20than%20doing%20it%20manually.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1827502%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%2FVBA%20code%20to%20duplicate%20every%20N-th%20row%2C%20N%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1827502%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F845723%22%20target%3D%22_blank%22%3E%40ushbar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20InsertRows()%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20m%20%3D%20Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20m%20%3D%20((m%20-%201)%20%5C%2017)%20*%2017%20%2B%201%0A%20%20%20%20For%20r%20%3D%20m%20To%2018%20Step%20-17%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r).EntireRow.Copy%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r%20%2B%201).Resize(4).EntireRow.Insert%0A%20%20%20%20%20%20%20%20Range(%22I%22%20%26amp%3B%20r%20%2B%201).Value%20%3D%20%22Paran%C3%B8tter%22%0A%20%20%20%20%20%20%20%20Range(%22I%22%20%26amp%3B%20r%20%2B%202).Value%20%3D%20%22Pekann%C3%B8tter%22%0A%20%20%20%20%20%20%20%20Range(%22I%22%20%26amp%3B%20r%20%2B%203).Value%20%3D%20%22Pistasjn%C3%B8tter%22%0A%20%20%20%20%20%20%20%20Range(%22I%22%20%26amp%3B%20r%20%2B%204).Value%20%3D%20%22Valn%C3%B8tter%22%0A%20%20%20%20Next%20r%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hi,

 

I'm a newbie to VBA and macro so I need help with a script/code that can duplicate every 17th row, four number of times. This should be a loop.

 

I have attached a part of my dataset. I want to duplicate every 17th row because I have a new 'Id' after every 17th row. 

 

This is a bit urgent so I'm really trying to automate this process. Thanks for any help you can provide!

 

3 Replies
Highlighted
Best Response confirmed by ushbar (Occasional Contributor)
Solution

@ushbar 

Here is a macro:

Sub InsertRows()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("A" & Rows.Count).End(xlUp).Row
    m = ((m - 1) \ 17) * 17 + 1
    For r = m To 18 Step -17
        Range("A" & r).EntireRow.Copy
        Range("A" & r + 1).Resize(4).EntireRow.Insert
    Next r
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
Highlighted

@Hans Vogelaar 

Thanks a lot for your quick response! That worked!

 

I actually want to replace the four new rows with the following values in the "Allergener" column:

Paranøtter
Pekannøtter
Pistasjnøtter
Valnøtter

 

And column L-O should be empty for the four new columns. 

This should again be a loop similar to the original post. 

 

Do you have any code for this?

The duplicated rows from the code above do not always have the same values because the 17th row sometimes have a different value throughout the dataset. That's why a code would be more efficient than doing it manually.

Highlighted

@ushbar 

Sub InsertRows()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("A" & Rows.Count).End(xlUp).Row
    m = ((m - 1) \ 17) * 17 + 1
    For r = m To 18 Step -17
        Range("A" & r).EntireRow.Copy
        Range("A" & r + 1).Resize(4).EntireRow.Insert
        Range("I" & r + 1).Value = "Paranøtter"
        Range("I" & r + 2).Value = "Pekannøtter"
        Range("I" & r + 3).Value = "Pistasjnøtter"
        Range("I" & r + 4).Value = "Valnøtter"
    Next r
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub