+ Reply to Thread
Results 1 to 4 of 4

Macro to insert a row.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2013
    Location
    white river,south africa
    MS-Off Ver
    2010
    Posts
    9

    Macro to insert a row.

    Hi. I have 2 sheets with similar data.
    sheet1 Sheet2
    101 101
    102 104
    103
    104
    I need the macro to compare the data in sheet 1 and 2 and insert lines in sheet 2 until data match to look like this.
    sheet1 sheet2
    101 101
    102
    103
    104 104


    Thanks

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to insert a row.

    Assuming:

    1) Data on sheet1 is in column A starting at A1
    2) Data on sheet2 is in column A starting at A1
    3) Data on sheet2 may not be in the correct order to match sheet1 yet
    4) All Data on sheet2 is on sheet1, but not all data on sheet1 is on sheet2

    --------------
    Approach:

    1) Add a blank sheet
    2) Add a formula to new sheet that does a simple ISNUMBER(MATCH()) test to create the new expanded list using only the values from sheet1, but matching to sheet2 (trust me)

    Option Explicit
    
    Sub SpaceSheet2Data()
    Dim LR As Long
    
    LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    With Sheets.Add
        With .Range("A1:A" & LR)
            .FormulaR1C1 = "=IF(ISNUMBER(MATCH(Sheet1!RC, Sheet2!C, 0)), Sheet1!RC, """")"
            .Copy
        End With
        
        Sheets("Sheet2").Range("A1").PasteSpecial xlPasteValues
        Application.DisplayAlerts = False
        .Delete
    End With
    
    End Sub
    3) Copy the new list of values from the new sheet to sheet2 replacing the original data
    4) Delete the added sheet
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-04-2013
    Location
    white river,south africa
    MS-Off Ver
    2010
    Posts
    9

    Re: Macro to insert a row.

    Thanks so much it works 100%

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to insert a row.

    I've marked this thread as SOLVED for you.
    Next time, select Thread Tools from the links above to mark a thread as SOLVED. Thanks.

+ 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] Macro to copy row, insert below, invert amt, and insert blank row between each new tx
    By raquel1227 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-07-2013, 12:56 PM
  2. Insert blank row, sum and insert page break macro
    By kim5012 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2012, 01:46 AM
  3. Macro: Copy Row and Insert it ; Insert Odd Year
    By buto87 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-25-2012, 08:55 AM
  4. Insert row macro? or inputbox macro?
    By Santiago11 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2011, 06:28 AM
  5. Macro to insert copy and insert formulas only to next blank row
    By bob in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2006, 07:10 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