+ Reply to Thread
Results 1 to 9 of 9

Macro to Append Data

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Macro to Append Data

    In need of macro that will do the following:

    (1) Copy data from Book1.Worksheet1 in range a1:j2
    (2) Paste values into Book2.Worksheet1 in range a1:j2

    The values in (1) refresh periodically. When they do, I'd like to be able to hit the macro button proceed to (2) again, but this time the data will append to the bottom.

    Thanks in advance for the help.
    Last edited by ExcelQ1; 12-01-2010 at 05:36 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Append Data

    Hi ExcelQ1

    Try this
    Option Explicit
    Sub UpDate_Book2()
        Dim wbData As Workbook
        Dim wbTarget As Workbook
        Dim wsData As Worksheet
        Dim wsTarget As Worksheet
        Dim LR As Long
        Dim MyPath As String
        Application.ScreenUpdating = False
        MyPath = ActiveWorkbook.Path
        Set wbData = ThisWorkbook
        Application.Workbooks.Open (MyPath & "\" & "Book2.xls")
        Set wbTarget = Workbooks("Book2.xls")
        For Each wsData In wbData.Sheets
            For Each wsTarget In wbTarget.Sheets
                If wsTarget.Name = wsData.Name Then
                    LR = wsTarget.Range("A" & Rows.Count).End(xlUp).Row
                    If Not LR = 1 Then LR = LR + 1
                    wsData.Range("A1:J2").Copy Destination:=wsTarget.Range("A" & LR)
                    Exit For
                End If
            Next wsTarget
        Next wsData
        wbTarget.Close savechanges:=True
        Application.ScreenUpdating = True
    End Sub
    Let me know of issues.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    11-24-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to Append Data

    Thank you, John. That helped tremendously. I do have one follow-up question...
    In the aforementioned code, we export the range "A1:J2" every time the macro is executed.

    Within that range, A1:J1 is merely a header row. The first time the macro is executed, I'd like it to export the entire "A1:J2" range (including header row). However, for every subsequent execution I only want to export the range "A2:J2" because I do not need to repeat the header row. Any thoughts on how I do this?

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Append Data

    Hi ExcelQ1

    See if this works for you
    Option Explicit
    Sub UpDate_Book2()
        Dim wbData As Workbook
        Dim wbTarget As Workbook
        Dim wsData As Worksheet
        Dim wsTarget As Worksheet
        Dim LR As Long
        Dim MyPath As String
        Application.ScreenUpdating = False
        MyPath = ActiveWorkbook.Path
        Set wbData = ThisWorkbook
        Application.Workbooks.Open (MyPath & "\" & "Book2.xls")
        Set wbTarget = Workbooks("Book2.xls")
        For Each wsData In wbData.Sheets
            For Each wsTarget In wbTarget.Sheets
                If wsTarget.Name = wsData.Name Then
                    wsData.Range("A1:J1").Copy Destination:=wsTarget.Range("A1")
                    LR = wsTarget.Range("A" & Rows.Count).End(xlUp).Row + 1
                    wsData.Range("A2:J2").Copy Destination:=wsTarget.Range("A" & LR)
                    Exit For
                End If
            Next wsTarget
        Next wsData
        wbTarget.Close savechanges:=True
        Application.ScreenUpdating = True
    End Sub
    John

  5. #5
    Registered User
    Join Date
    01-22-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Macro to Append Data

    Hi John,

    I used below code to append data from one workbook to another, but it's pasting the data into the target workbook. I copied the code without Sub UpDate_Book2() line, could that be the problem?

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Append Data

    Hi MacroLover

    Welcome to the Forum!

    Unfortunately, Rule # 2 reads... Don't post a question in the thread of another member -- start your own. If you feel it's particularly relevant, provide a link to the other thread.

  7. #7
    Registered User
    Join Date
    11-24-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to Append Data

    Excellent! This is just what I need. Thank you so much.

  8. #8
    Registered User
    Join Date
    06-29-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Macro to Append Data

    Hello, looking for help with macro code to append a list of neighborhoods to a list of keywords. For example:

    Neighborhood Names:

    Mountainbrook
    Myers Park
    Nevin Community
    Newell
    Newell South

    Keywords:
    keyword1
    keyword 2
    keyword 3

    Results:
    Mountainbrook keyword1
    Myers Park keyword1
    Nevin Community keyword1
    Newell keyword1
    Newell South keyword1
    Mountainbrook keyword2
    Myers Park keyword2
    Nevin Community keyword2
    Newell keyword2
    Newell South keyword2
    Mountainbrook keyword3
    Myers Park keyword3
    Nevin Community keyword3
    Newell keyword3
    Newell South keyword3

    In addition, would like to output the reverse scenario:

    keyword1 Mountainbrook
    keyword1 Myers Park
    keyword1 Nevin Community
    keyword1 Newell
    keyword1 Newell South
    keyword2 Mountainbrook
    keyword2 Myers Park
    keyword2 Nevin Community
    keyword2 Newell
    keyword2 Newell South
    keyword3 Mountainbrook
    keyword3 Myers Park
    keyword3 Nevin Community
    keyword3 Newell
    keyword3 Newell South

    I have the data on separate tabs in a spreadsheet - one tab for keywords (column b) and one tab for neighborhood names (column a).

    Thanks!
    Tiffany

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Append Data

    Hi Tiffany
    Welcome to the Forum!

    Unfortunately, Rule # 2 reads... Don't post a question in the thread of another member -- start your own. If you feel it's particularly relevant, provide a link to the other thread.

    Please start your own thread. If you wish, you can PM me with a link and I'll look at it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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