+ Reply to Thread
Results 1 to 14 of 14

Importing and Exporting data from workbook to workbook in excel

  1. #1
    Registered User
    Join Date
    01-02-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2003
    Posts
    22

    Importing and Exporting data from workbook to workbook in excel

    Hi all,

    I'm after a code that will allow me to export data in one worksheet to a worksheet on a central server, and then a separate macro in another workbook that will allow me to import the data that is now housed on the central server.

    The data being copied it the entire sheet, so from A1 to V196.

    Any help would be greatly appreciated.

    Thanks,

    Daniel.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing and Exporting data from workbook to workbook in excel

    Would you be downloading the file from the central server, copying the data and then uploading it again? Or would you directly prefer accessing it through the macro?

    The reason i ask this is, sometimes when the server is in a different location, it takes a considerable amount of time for even a simple copy paste to happen. From my previous experience, i worked from India while the server was maintained in US. We had to download the file first and then work on the file instead of directly accessing it since it took a long time to open.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Importing and Exporting data from workbook to workbook in excel

    To 'import' the data: use a databasequery



  4. #4
    Registered User
    Join Date
    01-02-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Importing and Exporting data from workbook to workbook in excel

    Thanks for the replies

    arlu1201 - I would like the macro to do it, the server is close by so that wont be an issue, I wont be re uploading once the data has been copied. I plan to use the central server workbook just to house the data and which the first workbook will upload to and that the second book will import from. The data will be overwritten in the central server workbook whenever it is uploaded, with the new uploaded data from the first workbook.

    snb - Thanks, I will check that out

  5. #5
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Importing and Exporting data from workbook to workbook in excel

    Hi D
    .
    Below is some code and an attached WB that uses ADO to get data from a Source WB and send
    it to a Target WB
    .
    AssumptionsCode can be modified to adjust these)
    .
    This WB (Manager.xlsm) will:
    Get data from a WB name Source.xlsx
    Send the data to a WB named Target.xlsx

    Code:
    Alt+F11
    CTR+R
    Double Click on Module1
    ENTER Subroutine:
    Public Sub Main_From_SourceToTarget()

    Required:
    A WB name SOURCE.XLSX in the SAME directory as this WB
    A WB name TARGET.XLSX in the SAME directory as this WB

    Code Assumptions:
    Assumes that ALL THE WB's are Excel 2007
    Assumes that the Source WB has data on a WS named Sheet1

    Assumes TOOLS -> REFERENCES
    MicroSoft ActiveX Data Objects 2.8 Library
    MicroSoft ActiveX Data Objects Recordset 2.8 Library
    MicroSoft ADO Ext 2.8 for DDL and Security

    Please Login or Register  to view this content.
    Attached is the WB named Manager. This code has been tested.

    hth
    regards
    John
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-02-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Importing and Exporting data from workbook to workbook in excel

    Hi John,

    Thank you very much for your detailed response, I very much appreciate it.

    I'm having a slight problem, i keep getting a "52 Bad file or number error"

    Public Sub Main_From_SourceToTarget()

    'http://www.excelforum.com/excel-programming/808336-importing-and-exporting-data-from-workbook-to-workbook-in-excel.html

    On Error GoTo EH_Main_From_SourceToTarget

    Dim lng As Long
    Dim i As Integer

    '-------------------
    '-------------------
    ms_SubName = "Public Sub Main_From_SourceToTarget()"

    '-------------------
    ' This WB
    '-------------------
    Set mo_WBThis = ThisWorkbook
    ms_WBThisVersion = Application.Version
    ms_WBThisPath = ThisWorkbook.Path

    '-------------------
    ' WB Source
    '-------------------
    ms_WBSourceName = "Source.xlsx" '<------- Change to real after testing this
    ms_WBSourcePath = ms_WBThisPath & "C:\Documents and Settings\frances\Desktop\New Folder\Source.xlsx" '<------- Change to real after testing this
    ms_WBSourceFullName = ms_WBSourcePath & ms_WBSourceName

    '-------------------
    ' WB Target
    '-------------------
    ms_WBTargetName = "Target.xlsx" '<------- Change to real after testing this
    ms_WBTargetPath = ms_WBThisPath & "C:\Documents and Settings\frances\Desktop\New Folder\Target.xlsx" '<------- Change to real after testing this
    ms_WBTargetFullName = ms_WBTargetPath & ms_WBTargetName

    '-------------------
    ' Check WB Source EXISTS
    '-------------------
    mb_Bool = IsFile(ms_WBSourceFullName)
    If Not mb_Bool Then
    MsgBox "File " & ms_WBSourceFullName & vbCrLf & vbCrLf & "Does not exist." & vbCrLf & vbCrLf & "Terminating ................ ", vbCritical, ms_SubName
    Exit Sub
    Else
    '-------------------
    ' IGNORE
    ' FYI - Not Necessary but works
    '-------------------
    If 1 = 2 Then
    Set mo_WBSource = Application.Workbooks.Open(ms_WBSourceFullName)
    End If
    End If

    '-------------------
    ' Check WB Target EXISTS
    '-------------------
    mb_Bool = IsFile(ms_WBTargetFullName)
    If Not mb_Bool Then
    MsgBox "File " & ms_WBTargetFullName & vbCrLf & vbCrLf & "Does not exist." & vbCrLf & vbCrLf & "Terminating ................ ", vbCritical, ms_SubName
    Exit Sub
    Else
    '-------------------
    ' FYI - Not Necessary but works
    '-------------------
    If 1 = 2 Then
    ' IGNORE
    Set mo_WBTarget = Application.Workbooks.Open(ms_WBTargetFullName)
    End If
    I have changed the file names to Source and Target, and source has a sheet named Sheet1, are in the same directory as the file you gave me and I cannot work out for the life of me why this error is coming up!

  7. #7
    Registered User
    Join Date
    01-02-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Importing and Exporting data from workbook to workbook in excel

    Also, just to add, I think i will need the target work book to have a macro to import the data as opposed to being exported to as the target file name will change.

    I think it will need to work like so : Source ---->export to Central workbook------>Target Imports from central workbook.

    Sorry for being so demanding, I'm not even sure it this is possible!

    Daniel

  8. #8
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Importing and Exporting data from workbook to workbook in excel

    @John,
    For my own interest, why do you have the references to
    MicroSoft ActiveX Data Objects Recordset 2.8 Library
    MicroSoft ADO Ext 2.8 for DDL and Security

    since your code does not appear to use anything from either library? (did you just adapt this from an existing project that did use those?)
    Good luck.

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing and Exporting data from workbook to workbook in excel

    I guess all you need is this -
    Code to export the data to the server -
    Please Login or Register  to view this content.
    Code to import the data from the server -
    Please Login or Register  to view this content.
    Edit the cell references as necessary. While using the 2nd code, if you need to replace the existing data, you can add this line after the End if -
    Please Login or Register  to view this content.
    Last edited by arlu1201; 01-08-2012 at 08:33 AM.

  10. #10
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Importing and Exporting data from workbook to workbook in excel

    Hi Dans

    Perspective:

    What we want to do first is PROVE the feasibility of the solution I gave you. It is _always_ possible
    that I gave you a _bad_ solution. Once we PROVE the feasibility of what you have - then - it is (I think)
    just a matter of changing the file names. And that means we can SWING both ways - so don't worry
    about getting the data FROM:Server File TO:Local File. but _FIRST_ we have to PROVE feasibility.

    (1) Are you using Excel 2007 -OR- Excel 2003?

    If you are using Excel 2003 - my code is __BAD__ and I have to change it. Its a No brainer.

    (2) Have you and are you comfortable with putting your cursor inside the routine and F8 thru
    the code. This means you are walking down the code and we can SEE which line the code
    BLOWS UP?

    (3) For right now - we are ONLY interested in creating two DUMMY Excel files.

    One file is called SOURCE.XLSX and one file is called TARGET.XLSX. I did not post this
    but you would put some dummy data on SOURCE.XLSX on sheet 1.

    (4) If you meet all the conditions above (Excel 2007) then go into the routine and F8 thru
    the code and please tell me WHAT LINE of code blows up. By this I mean - an error will occur
    on the line - and the code will immediately goto the ERROR routine at the very end of the
    sub routine.

    If I am confusing you please YELL at me and we will try again. This stuff is _not_ hard and
    we will get there.

    regards
    John

  11. #11
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Importing and Exporting data from workbook to workbook in excel

    Hi ON

    You are CORRECT.

    As written - the code does NOT required all 3 references.

    These 2 are NOT required. (As you have pointed out)
    MicroSoft ActiveX Data Objects Recordset 2.8 Library
    MicroSoft ADO Ext 2.8 for DDL and Security

    My reason for putting them there would sound like a cop-out so best to say they are NOT needed.

    regards
    John

  12. #12
    Registered User
    Join Date
    01-02-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Importing and Exporting data from workbook to workbook in excel

    Thanks again for the replies,

    Arlu, your code worked for me, is there anyway I can automate the file paths in the code, is there away to make it so that i wont have to choose where the file is located? The file being exported to and imported from does not change location or name.

    John, I will go through it later and get back to you.

    Thanks,

    Daniel

  13. #13
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Importing and Exporting data from workbook to workbook in excel

    Hi dan

    Arlu's code looks good and is easier to manage. I would hang with it.

    No need to get back to me.

    regards
    John

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Importing and Exporting data from workbook to workbook in excel

    Its simple. I have changed 1 of the codes, you can change the 2nd one.
    Please Login or Register  to view this content.

+ 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