+ Reply to Thread
Results 1 to 11 of 11

Insert a blank row after based upon a condition

  1. #1
    Registered User
    Join Date
    04-09-2014
    Location
    Sweden
    MS-Off Ver
    MS 365 version 2302
    Posts
    18

    Cool Insert a blank row after based upon a condition

    Hi all,

    I am quite new with regards to excel marcos and VBA. I am trying to build a macro that adds a new line below a column contains a specific value.

    For example when column J contains "615006" I would like to add a line below that line and if possible also copy the entire previous line but replaced the value in column J with value "615006".

    See attached file for more details.

    Mikael
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    07-14-2011
    Location
    Ha Noi, Viet Nam
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Insert a blank row after based upon a condition

    Try this :

    PHP Code: 
    Sub InsRow()
    Dim i As Long
    For 21 To 1 Step -1
        
    If Cells(i10).Value 615006 Then
            Cells
    (i10).EntireRow.Insert
        End 
    If
    Next
    End Sub 

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Insert a blank row after based upon a condition

    Maybe:

    Please Login or Register  to view this content.
    Note: Your sample seems to insert a line above?

  4. #4
    Registered User
    Join Date
    04-09-2014
    Location
    Sweden
    MS-Off Ver
    MS 365 version 2302
    Posts
    18

    Re: Insert a blank row after based upon a condition

    Thanks, this one did the trick:

    Sub mm82()
    Dim i As Long
    For i = Range("J" & Rows.count).End(3)(1).Row To 2 Step -1
    If Range("J" & i).Value = 615006 Then
    Range("J" & i + 1).EntireRow.Insert
    Range("J" & i + 1).Value = Range("J" & i).Value
    End If
    Next i
    End Sub

    A couple of more questions:

    when a new line is added I want to that the new line should have colmn J equal for example xxx and all other colmn values copied from the previous line, is that also possible?

    is it possible to dynamically configure this so that I get a dialog that asks me to select which column to search by marking that column? and also what to search for (in my case 615006) and what the new should be in that column? in total three dialogs?

  5. #5
    Registered User
    Join Date
    04-09-2014
    Location
    Sweden
    MS-Off Ver
    MS 365 version 2302
    Posts
    18

    Re: Insert a blank row after based upon a condition

    I have about 30000 lines that I need search, is that possible as well?

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Insert a blank row after based upon a condition

    Try:

    Please Login or Register  to view this content.
    Not sure what this means "what the new should be in that column?". So I may have misunderstood.

  7. #7
    Registered User
    Join Date
    04-09-2014
    Location
    Sweden
    MS-Off Ver
    MS 365 version 2302
    Posts
    18

    Re: Insert a blank row after based upon a condition

    You understood me correctley!

    How do I change it so I just mark the column in the spread sheet that I want to search in instead of typing it? Also when I search for example the values in a column they are "spaces" after.. so if I search for "615006" I will not get a hit, but if I enter "615000 " I get a hit?

    The last one, when I copy your marcro and save it the sheet as *xlsm the macro is not avaliable the next time I open the file??

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Insert a blank row after based upon a condition

    You would need to select the column first.

    Please Login or Register  to view this content.

    "The last one, when I copy your marcro and save it the sheet as *xlsm the macro is not avaliable the next time I open the file??"

    I don't know what's going on there. I only have 2003. Perhaps create an xlsm file and then copy and paste? I dunno.

  9. #9
    Registered User
    Join Date
    04-09-2014
    Location
    Sweden
    MS-Off Ver
    MS 365 version 2302
    Posts
    18

    Re: Insert a blank row after based upon a condition

    Thank you for all the help john! Made my day

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Insert a blank row after based upon a condition

    You're welcome. Glad to help out and thanks for the feedback. Please comply with Forum Rule No. 9.


    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Insert a blank row after based upon a condition

    Hi, mm82,

    your post #4 does not comply with our Forum Rules #3 - you should wrap your procedure with code tags.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. Insert Row and Values based upon condition
    By btamulis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2014, 01:52 PM
  2. vba code to insert blank row after specific condition is met
    By archies.gall in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2014, 05:07 AM
  3. Need to insert Rows Based on a Condition
    By skandkamat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-17-2013, 12:06 AM
  4. [SOLVED] Macro to Insert blank rows based on condition
    By Nikkishr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2012, 03:32 AM
  5. Insert row based on condition
    By Letthew in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-21-2009, 08:39 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