+ Reply to Thread
Results 1 to 6 of 6

How to insert row(s) after certain set of data

Hybrid View

  1. #1
    Registered User
    Join Date
    05-19-2022
    Location
    chennai
    MS-Off Ver
    2016
    Posts
    7

    How to insert row(s) after certain set of data

    Hi,

    I have around 2-3K of line items and I would like to insert new rows after certain lines.

    So, in my file I have data in column F - "Reference" and column G "Account type" . Taking both as my parameter i will have to insert a row in the next line.

    For example, in first row, I have "TA908244/AB1" as reference and account type as "AP" , the second row has "TA908244/AB1" but the account type is "GL". So, here I will consider both the rows as one data and insert a new row in the third row. In this example there is only one "GL" row but other cases for one reference value there can be multiple "GL" rows

    Overall the logic is that after each "Reference value" I will need to insert a row.

    I have attached sample file for reference. Tab 1 is the raw data and tab 2 has the format in which I want the output.
    Sample.xlsx
    Please let me know if there is anyway to do this. Thanks in advance.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to insert row(s) after certain set of data

    You can insert above 34 in column J

    Sub InsertAbove34()
        Dim ws As Worksheet
        Dim cnt As Long
        Dim x
    
        Set ws = ActiveSheet
    
        With ws
            cnt = .Cells(.Rows.Count, "J").End(xlUp).Row
            For x = cnt To 3 Step -1
                If .Cells(x, "J") = 34 Then Rows(x).Insert
            Next
        End With
    
    
    
    End Sub
    Last edited by davesexcel; 03-25-2023 at 10:49 AM.

  3. #3
    Registered User
    Join Date
    05-19-2022
    Location
    chennai
    MS-Off Ver
    2016
    Posts
    7

    Re: How to insert row(s) after certain set of data

    Thanks very much this was really helpful.

    The code has accomplished almost what I needed, except in reference value containing "/AB2" should also be moved to different row like other data rows. In the attached image I have highlighted in red Attachment 823159

    Also the last line , I see one row data alone hangs separate, in the same image I have highlighted in Yellow. If there is something that be done then it would be helpful. If not then that's fine
    Attached Files Attached Files

  4. #4
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: How to insert row(s) after certain set of data

    maybe something like this with Power Query

    // Table1
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        TCN = List.RemoveLastN(Table.ColumnNames(Source),1),
        DateLoc1 = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-US"),
        DateLoc2 = Table.TransformColumnTypes(DateLoc1, {{"Date2", type date}}, "en-US"),
        IF = Table.AddColumn(DateLoc2, "IF", each if [Typer] = "SM" then 14.5 else [Help]),
        Grp = Table.Group(IF, {"IF"}, {{"All", each _, type table}}, GroupKind.Local),
        BlankRows = Table.TransformColumns(Grp, {{"All", each Table.InsertRows(_, Table.RowCount(_),
            {[
                Date = null,
                Date2 = null,
                Typer = null,
                Text = null,
                ReferenceValue = null,
                AccountType = null,
                Account = null,
                C = null,
                Amount = null,
                Key = null,
                Help = null,
                IF = null
            ]})
        }}),
        TSC = Table.SelectColumns(BlankRows,{"All"}),
        Exp = Table.ExpandTableColumn(TSC, "All",TCN),
        Date = Table.TransformColumnTypes(Exp,{{"Date", type date}, {"Date2", type date}})
    in
        Date
    Last edited by sandy666; 03-25-2023 at 10:38 PM.

  5. #5
    Registered User
    Join Date
    05-19-2022
    Location
    chennai
    MS-Off Ver
    2016
    Posts
    7

    Re: How to insert row(s) after certain set of data

    Thanks very much for the help. How do I go about inserting this query in my file? I tried converting the data to table and then inserting via Power Query but didn't work.

  6. #6
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: How to insert row(s) after certain set of data

    • copy M from the post
    • select your range
    • use Ctrl+T to change range to table
    • add formula as last column (Help)
    • Data tab - from table
    • Advanced Editor - replace all there with copied M
    • name of the table (eg. Table1) must be the same as in M - fix it manually if necessary
    • headers of the table must be the same as in M

    if you don't know Power Query will be hard if something goes wrong so try to read Power Query documentation first

+ 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] How to insert insert multiple rows between each existing row and past in data
    By Lojinxx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2022, 07:31 AM
  2. Insert blank rows when data changes and calculate how many rows to insert
    By Smartmart in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-23-2020, 01:04 PM
  3. [SOLVED] Insert Xnumber (X=A1) of rows between data in a table& copy data from a range to new cells
    By blue_clouds_mountain in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-09-2019, 12:47 PM
  4. Keywords needed- Import data then Macros insert specific data into format
    By jshephe2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-26-2016, 03:20 PM
  5. [SOLVED] Loop through entire sheet, Insert row, copy row data, paste row data, delete row
    By Hyperdude in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2012, 01:46 AM
  6. Copy paste data, insert rows, insert data and change . to -
    By xWiZardx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2010, 01:44 AM
  7. Replies: 5
    Last Post: 09-15-2008, 07:04 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