+ Reply to Thread
Results 1 to 7 of 7

Transfer/Copy Data from one workbook to another

Hybrid View

scottw1988 Transfer/Copy Data from one... 10-26-2012, 08:35 PM
FDibbins Re: Transfer/Copy Data from... 10-26-2012, 08:37 PM
jeffreybrown Re: Transfer/Copy Data from... 10-26-2012, 08:42 PM
scottw1988 Re: Transfer/Copy Data from... 10-26-2012, 08:53 PM
scottw1988 Re: Transfer/Copy Data from... 10-26-2012, 08:48 PM
jeffreybrown Re: Transfer/Copy Data from... 10-26-2012, 09:17 PM
abousetta Re: Transfer/Copy Data from... 10-26-2012, 09:46 PM
  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Louisville, KY
    MS-Off Ver
    Excel 2010
    Posts
    5

    Transfer/Copy Data from one workbook to another

    Hello all,

    I am currently writing macros that will transfer data from a template file, to what is essentially a log file. How does one go about copying data from one workbook to another. I have currently been able to transfer data from one sheet to another using the following code:

    Sub TestMacro3()
    '
    ' TestMacro2 Macro
    '
    
    '
        Range("B1").Select
        Selection.Copy
        Sheets("Sheet2").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(0, 1).Range("A1").Select
        Sheets("MONTHLY REPORT").Select
        Range("B2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(0, 1).Range("A1").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "=YEAR(TODAY())"
        ActiveCell.Offset(1, 0).Range("A1").Select
    End Sub
    How could I get this code to write to another workbook. I believe that I read in order to paste another file it must be opened. Is that correct? Any help would be much appreciated.

    Thanks,
    Scott

    Moderator's note: code tags added for you
    Last edited by FDibbins; 10-26-2012 at 08:38 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,049

    Re: Transfer/Copy Data from one workbook to another

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Transfer/Copy Data from one workbook to another

    Hi Scott,

    Instead of trying to read this code, it would help if you just laid out the particular's.

    Can we start with what you are trying to accomplish?
    HTH
    Regards, Jeff

  4. #4
    Registered User
    Join Date
    10-24-2012
    Location
    Louisville, KY
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Transfer/Copy Data from one workbook to another

    I should also note that I need the macros to work with any workbook. The reason being that there are 12 different files that I need to pull from (one for each month). I will be pulling the info from the exact same cells each time. I really just need to know where and how to name the source WorkBook in a manner that whenever the i.e. (January, February, March etc) workbook files are being used it can still paste the correct values into the destination file which will not change.

  5. #5
    Registered User
    Join Date
    10-24-2012
    Location
    Louisville, KY
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Transfer/Copy Data from one workbook to another

    Hi Jeff,

    I am trying to write a macro that will copy/paste certain cells from one workbook to another workbook. So basically, the values entered in WB1 I want transfered to WB2.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Transfer/Copy Data from one workbook to another

    There are many ways to go with this that is why we look for specifics up front.

    Here is an example I worked up, but with the additional info you posted it will take some more effort (at least for me).

    Option Explicit

    Sub MoveData()
        Dim MyPath  As String
        Dim MyFile  As String
        Dim fNAME   As String
        Dim i       As Long
        ''''''''''''''''''''''''''''''''''''''''''''''''
        'Make sure workbook/worksheet names match
        Dim WB1     As String: WB1 = "wb1.xlsm"
        Dim WB2     As String: WB2 = "wb2.xlsm"
        Dim wsSrc   As String: wsSrc = "Sheet1"
        Dim wsDest  As String: wsDest = "Sheet1"
        ''''''''''''''''''''''''''''''''''''''''''''''''
        Dim Rng     As Range
        Dim LR      As Long
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
    
        MyPath = ActiveWorkbook.Path & Application.PathSeparator
        MyFile = WB1
        fNAME = Dir(MyPath & MyFile)
    
        Workbooks.Open Filename:=MyPath & WB2
    
        With Workbooks(fNAME).Sheets(wsSrc)
            .Range("A1").Copy Workbooks(WB2).Sheets(wsDest).Range("A1")
        End With
    
        With Workbooks(WB2)
            Application.EnableEvents = False
            .Close SaveChanges:=True
            Application.EnableEvents = True
        End With
        
        With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
    End Sub

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Transfer/Copy Data from one workbook to another

    Hi Scott,

    Maybe this example can help.

    I have a workbook that is called AllYear and two other workbooks (Jan and Feb). I want to extract the exact cell from both Jan and Feb and put them in AllYear. Just put all the files on your desktop and the macro should recognize them.

    Option Explicit
    
    Sub test()
    
    Dim MyMonths
    Dim m%, r%
    Dim wbJan$, wbFeb$
    
    wbJan = CreateObject("WScript.Shell").SpecialFolders("DeskTop") & "Jan.xlsx"    ' You can replace this with an actual file path
    wbFeb = CreateObject("WScript.Shell").SpecialFolders("DeskTop") & "Feb.xlsx"    ' You can replace this with an actual file path
    
      MyMonths = Array(wbJan, wbFeb)
    
        For m = LBound(MyMonths) To UBound(MyMonths)
        
            r = r + 1
            Workbooks.Open Filename:=MyMonths(m)
            ThisWorkbook.Sheets(1).Cells(r, 1) = ActiveWorkbook.Sheets(1).Cells(1, 1)
            ActiveWorkbook.Close False
        
        Next
    
    End Sub
    Let me know if things are more clear now.

    abousetta
    Attached Files Attached Files
    Last edited by abousetta; 10-26-2012 at 10:29 PM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

+ 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