+ Reply to Thread
Results 1 to 13 of 13

Copy data from one workbook and paste to another workbook beside the specified date

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Copy data from one workbook and paste to another workbook beside the specified date

    Hello Everyone,

    I'm cracking my head over VBA codes that would help me, copy a set of data from a specific range in a workbook and paste it to another workbook beside the dates in a row. I quite spent some time looking for solutions but most of the answers were for sheet to sheet transfers.
    Ideally what I'm trying to do is

    1. Copy data from customer workbook range A2:F5000 from Sheet 1
    2. Activate target workbook and Sheet 1 and search in column B for the date that is equal to a date specified in Cell A1 in the target workbook
    3. Paste the data in the target workbook beside the corresponding date
    4. Save and close the customer workbook.

    I have my code below but the search and the paste part is my issue.

    I would appreciate it if someone could help me.Thanks

    Sub Import()

    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook

    ' make assumption that active workbook is the target
    Set targetWorkbook = Application.ActiveWorkbook

    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)

    Set customerWorkbook = Application.Workbooks.Open(customerFilename)

    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)

    targetSheet.Range("A2", "F50000").Value = sourceSheet.Range("A2", "F50000").Value

    'should find search for a corresponding to a cell value in cell A1 in destination workbook and paste the data beside it

    ' Close customer workbook
    customerWorkbook.Close

    MsgBox "Imported"

    End Sub

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    To add a file to a new post

    To add a file to an existing post.

  3. #3
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Thanks Dave,

    I uploaded the sample file that I'm working on. Basically the data in customer workbook will be transferred to Target workbook.

    Thanks in advance.
    Attached Files Attached Files

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    You can edit your post to add code tags.

    AA CodeTags.jpg

  5. #5
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Anyone could help me figure out what to do?

    Sub Import()
    
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook
    
    ' make assumption that active workbook is the target
    Set targetWorkbook = Application.ActiveWorkbook
    
    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)
    
    Set customerWorkbook = Application.Workbooks.Open(customerFilename)
    
    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)
    
    targetSheet.Range("A2", "F50000").Value = sourceSheet.Range("A2", "F50000").Value
    
    'should find search for a corresponding to a cell value in cell A1 in destination workbook and paste the data beside it
    
    ' Close customer workbook
    customerWorkbook.Close
    
    MsgBox "Imported"
    
    End Sub
    thanks

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Try this,
    Sub Import()
    
        Dim customerBook As Workbook
        Dim filter As String
        Dim caption As String
        Dim customerFilename As String
        Dim customerWorkbook As Workbook
        Dim targetWorkbook As Workbook
        Dim sourceSheet As Worksheet
        Dim targetSheet As Worksheet
        Dim TargetRng As Range
        Dim FindRng As Range, crng As Range
        Dim FoundRng As Range
    
        Set targetWorkbook = ThisWorkbook
        Set targetSheet = targetWorkbook.Worksheets(1)
        Set FindRng = targetSheet.Range("A1")
        Set crng = targetSheet.Columns("B:B").SpecialCells(xlCellTypeConstants, 23).Find(what:=FindRng, lookat:=xlWhole)
    
        filter = "Text files (*.xlsx),*.xlsx"
        caption = "Please Select an input file "
        customerFilename = Application.GetOpenFilename(filter, , caption)
    
        If customerFilename <> "" Then
            Workbooks.Open Filename:=customerFilename
            Set customerWorkbook = ActiveWorkbook
        Else: MsgBox "Nothing Selected"
        End If
    
        Set sourceSheet = customerWorkbook.Worksheets(1)
        Set FoundRng = sourceSheet.Columns("B:B").SpecialCells(xlCellTypeConstants, 23).Find(what:=FindRng, lookat:=xlWhole)
    
        If Not FoundRng Is Nothing Then
            r = FoundRng.Row
            targetSheet.Range(targetSheet.Cells(crng.Row, "C"), targetSheet.Cells(crng.Row, "H")).Value = _
            Range(Cells(r, "C"), Cells(r, "H")).Value
        Else: MsgBox "Not Found"
            Exit Sub
        End If
    
        customerWorkbook.Close Save = True
        MsgBox "Imported"
    
    End Sub

  7. #7
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Hi Dave,

    thanks so much works fine, quick question how to I change the code to include the data from the other dates?
    such as include 15 October data and onward?

    14-10-2015 04-01-53.png

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    What other dates are you referring to? There is only 1 date in A1

  9. #9
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9
    Quote Originally Posted by davesexcel View Post
    What other dates are you referring to? There is only 1 date in A1
    Apologies for not being specific. What I mean was i need to copy allnthe data from the date in A1 and beyond. So if the date is 14 october data for 14 october and beyond will.copied ro the target sheet.

  10. #10
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Hi Dave,

    Any solution to the last request. I will be forever grateful to you if this will be solved.

    Thanks in advance

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    This should do it.
    
    
    Sub DoItWell()
        Dim wb As Workbook, bK As Workbook
        Dim ws As Worksheet, SH As Worksheet
        Dim f As Range
        Dim rng As Range, rng1 As Range, a As Range, c As Range
        Dim LstRw1 As Long, Lstrw2 As Long
        Application.ScreenUpdating = 0
        Set wb = Workbooks.Open("C:\Users\Dave\Downloads\Customer Workbooks.xlsx")
        Set bK = ThisWorkbook
        Set ws = wb.Sheets(1)
        Set SH = bK.Sheets(1)
        Set f = SH.Range("A1")
    
        LstRw1 = SH.Cells(SH.Rows.Count, "B").End(xlUp).Row
        Lstrw2 = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    
        ws.Range("B:B").AutoFilter Field:=1, Criteria1:=">=" & f
        SH.Range("B:B").AutoFilter Field:=1, Criteria1:=">=" & f
    
        Set rng = ws.Range("B1:B" & Lstrw2).SpecialCells(xlCellTypeVisible)
        Set rng1 = SH.Range("B1:B" & LstRw1).SpecialCells(xlCellTypeVisible)
    
        For Each c In rng1.Cells
            For Each a In rng.Cells
                If a = c Then
                    SH.Range(SH.Cells(c.Row, "C"), SH.Cells(c.Row, "H")).Value = ws.Range(ws.Cells(a.Row, "C"), ws.Cells(a.Row, "H")).Value
                End If
            Next a
        Next c
    
        ws.AutoFilterMode = 0
        SH.AutoFilterMode = 0
        wb.Close True
    End Sub
    Last edited by davesexcel; 10-25-2015 at 10:51 AM.

  12. #12
    Registered User
    Join Date
    03-10-2015
    Location
    Philippines
    MS-Off Ver
    2010
    Posts
    9

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    Hi Dave,
    Sorry to be a bit annoying, but the code seems to copy only the column headers and not the data.

    Thanks again.

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

    Re: Copy data from one workbook and paste to another workbook beside the specified date

    I would say it copies the column headers as well, not only the headers.

    Change the ranges to B2 instead of B1

+ 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 find data in source workbook and copy paste to target workbook
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-23-2014, 06:21 AM
  2. [SOLVED] Code to copy data from a closed workbook and paste in active workbook using named range.
    By paullie1912 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2014, 02:38 AM
  3. [SOLVED] Copy and Paste data from .xlsm workbook to .xls workbook
    By pinstripe05 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-04-2013, 11:54 AM
  4. Replies: 6
    Last Post: 01-29-2013, 07:01 AM
  5. [SOLVED] Copy/paste data from one workbook to another but workbook name variable
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-11-2013, 12:14 PM
  6. Replies: 3
    Last Post: 04-24-2012, 11:56 AM
  7. [SOLVED] Selecting data from 1 workbook to copy and paste to a 2nd workbook
    By JackSpam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2005, 10:05 PM

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