Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904

    Exporting to Excel

    I think I already know the answer to this one, but I am taking a short in the dark, because sometimes I am amazed at the functionality I am unaware of and the tricks people come up with.

    So, I am creating an Access database that loops through a list of records, build some SQL code, applies it to a query, and exports the query to Excel (using TransferSpreadsheet). So when all is said and done, I end up with a whole bunch of Excel files.

    My issue is that all these Excel files have their file widths set to the default 8.1 width, so you have to widen all the fields to actually read the data. Short of opening all the files after they are created in Excel and resizing the columns, is there any way to export the data to Excel and have it so the column-widths are auto-sized so people can read their files easily without having to adjust them themselves? This is going to be a bunch of managers, so the easier it is for them, the happier they are, and the better it is for me.



    I know it is probably a long-shot, but thought that I would ask.

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    So how about:
    - take one of the files with the columns the correct size, delete all the data, save it as something else
    - then in the code, copy this file to the new name before doing the export

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I am not quite sure I follow.
    What would the VBA of your suggestion look like?

    Note that the process is creating/exporting Excel files. It is not copying/pasting or anything like that.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    This is your statement:
    that loops through a list of records, build some SQL code, applies it to a query, and exports the query to Excel (using TransferSpreadsheet).

    Immediately before the last part, the export, create the file using FileCopy command. This is the file used in the export command.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    So let me see if I am understanding you correctly.

    Are you saying to do the following:
    - Create a blank Excel template file that has the column widths set as a like them
    - Use the FileCopy command to copy the the blank template over to the name of the file I want to export to
    - Export the query to the same file name that I just copied to

    If so, I have tried it, and it does not appear to be working. My TransferSpreadsheet command does not seem to like to export to an existing Excel file.
    Is there some trick in getting in getting TransferSpreadsheet to export to an existing file? It appears that it requires more than just having the existing file name in the File Name argument.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You have been around long enough to know that "not working" is unacceptable!! What exactly is not working?

  7. #7
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    But yes, that's exactly what I meant for you to do. Except how did you create the file - like I said?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    According to MS on TransferSpreadsheet: "Access creates a new spreadsheet when you export data from Access. If the file name is the same as the name of an existing spreadsheet, Access replaces the existing spreadsheet, unless you're exporting to an Excel version 5.0 or later workbook. In that case, Access copies the exported data to the next available new worksheet in the workbook."

    However, MS also says can edit linked spreadsheet with Access but I have never been able to do that.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Haven't tested this, but June does Excel allow two worksheets with the same name?

  10. #10
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    So, I was playing around with it some more, and discovered I made a mistake in making the changes, and that was the problem.
    I was using the same variable for my SQL criteria and file name, and had added ".xlsx" to the end of it (needed for the FileCopy part).
    When I did that, it changed my SQL criteria, so no records were being returned!

    So I modified it, and it works like a charm now!

    Thanks a lot for you help. I had never really used TransferSpreadsheet to export to existing Excel files before (always to new files), and didn't realize it was just that easy.

  11. #11
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    According to MS on TransferSpreadsheet: "Access creates a new spreadsheet when you export data from Access. If the file name is the same as the name of an existing spreadsheet, Access replaces the existing spreadsheet, unless you're exporting to an Excel version 5.0 or later workbook. In that case, Access copies the exported data to the next available new worksheet in the workbook."

    However, MS also says can edit linked spreadsheet with Access but I have never been able to do that.
    Interesting, that doesn't appear to be the behavior I am experiencing.

    My template has just 1 sheet named "Data", and I set the first three column widths.

    Here is my export block, that copies the template then exports to that copy:
    Code:
    '       Copy Excel template to export location
            exportFile = exportPath & fName & ".xlsx"
            FileCopy exportTemplate, exportFile
            
    '       Export query to Excel table
            DoCmd.TransferSpreadsheet acExport, 10, "qry75-Groups_Members_By_Mgr_EXPORT", exportFile, True, ""
    It exports the data to that file, on the Data tab, and my column widths are what I set in my template. So it is not creating any new worksheets in my workbook.
    (So it is working exactly as I would have hoped, though seems to be different than quote you cited).

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Now that is odd, I expected Excel to name the worksheet the same as the query name, hence my original instructions. But glad it is working.

  13. #13
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Now that is odd, I expected Excel to name the worksheet the same as the query name, hence my original instructions. But glad it is working.
    It does when exporting to a new file, but apparently not when exporting to an existing one.

  14. #14
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    THAT explains June's comment!

  15. #15
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    THAT explains June's comment!
    If it truly replaces it though, I wouldn't expect it to keep the old tab name or column formatting.
    It seems to be acting more like it is opening it and pasted into the existing sheet (which is what I want, but did not expect it would do that).

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 12-28-2012, 02:06 PM
  2. exporting to excel
    By akrylik in forum Access
    Replies: 3
    Last Post: 05-24-2012, 12:27 AM
  3. Exporting to Excel
    By Andrias14 in forum Access
    Replies: 3
    Last Post: 03-20-2012, 06:04 AM
  4. Exporting to Excel Help
    By Coffee in forum Import/Export Data
    Replies: 4
    Last Post: 07-18-2011, 07:37 AM
  5. Exporting to Excel
    By TheDeceived in forum Programming
    Replies: 0
    Last Post: 11-15-2010, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums