Results 1 to 8 of 8
  1. #1
    StreetFighter is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    3

    Question How to export from Access to Excel with the columns set to autofit for width? (Brand new to access!)


    Hello,

    I apologize if this is a dumb question as all this is new to me.

    All I'm wanting to do is have Microsoft Access export a query to Microsoft Excel with the columns autofit for width. I have seen that it can be done, but I'm far from understanding everything just yet. I've created a test database for myself and so far have the following code:

    Code:
    Private Sub Export_Selected_Data_to_Excel_Button_Click()
        
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Export Selected Query", Environ("USERPROFILE") & "\Desktop\ExportedData.xlsx", True
        
    
    
    End Sub
    I know it is simple as I am very new to Microsoft Access and coding/VBA at all. Right now I am happy as I have learned to export the query from my table to my desktop and it looks great so far.

    I would love to have the columns autofit to width, and also if it is possible I would love to have the top row frozen? I know that's asking a lot! I'm hoping someone can help!

    Thank you so much for your time!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    try:
    DoCmd.OutputTo acOutputTable, "qsMyQry", acFormatXLSX, "c:\temp\myufile.xlsx"

    tho it has less parameters on sheet names and multiple tabs.

  3. #3
    StreetFighter is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    3
    Hello Ranman256!

    Thanks for helping me out! I see you helped other people and just want to say thanks first of all!

    Do I copy past that exactly into my access code? It gives me an error when I do. Or do I have to change something to fit my own databse?

    Click image for larger version. 

Name:	Error.JPG 
Views:	16 
Size:	14.7 KB 
ID:	50496

    Would I change the "qsMyQry" to the Query name I'm using? I'm very new to this so I'm sorry if this sounds dumb! Below is how I have it at the moment:
    Click image for larger version. 

Name:	Access Code 1.JPG 
Views:	16 
Size:	29.3 KB 
ID:	50497

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    You would need to run some excel automation code on the exported data to autofit etc.
    You can record a macro in excel to do just that and amend to suit.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    StreetFighter is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    3
    Okay I guess should take my time and learn more as this seems above my head at this point. I have only learned any code or SQL or VBA as of two weeks now I was hoping the solution to autofit the columns was a simple one! I think for now I'll just manually autofit and freeze the top row in the exported excel file as it isn't too much trouble I was just hoping to automate it which would have been icing on my novice coding cake.

    Thank you!

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    googling "docmd.outputto" brings you to the likes of

    https://learn.microsoft.com/en-us/of...docmd.outputto

    Pretty sure that the first suggestion applies to where you have an Excel file with appropriately sized columns that you will output to. It may or may not be appropriate for your needs. Unfortunately those are not exactly clear.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    To provide formatting you would need to open the excel file from Access after you have created it, and run some VBA code.
    There is no way to do it automatically without using some VBA beyond that.

    However it's not really that difficult, it just requires some effort to understand the syntax required.

    I have a routine that takes any query and creates a Excel table from the results.
    It is not overly complicated. I don't have the code to hand (I'm on a new computer) but suspect I have posted it on here somewhere.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    See this link
    https://www.accessforums.net/showthr...t=Export+excel

    Post #4 has code to export and format - for resizing columns look for ‘autofit’ in the code

Please reply to this thread with any new information or opinions.

Similar Threads

  1. MS Access Selecting Columns to Export to Excel
    By wscwt01 in forum Import/Export Data
    Replies: 8
    Last Post: 02-11-2022, 12:03 PM
  2. Autofit Columns in Table
    By mp3909 in forum Access
    Replies: 5
    Last Post: 03-13-2019, 04:02 PM
  3. how to AUTOFIT columns
    By florida2001 in forum Access
    Replies: 3
    Last Post: 04-07-2014, 02:16 PM
  4. Export columns (table) from access to excel
    By dacodac in forum Programming
    Replies: 4
    Last Post: 01-30-2013, 04:27 AM
  5. Excel Automation Autofit Column (or Cell?) Width
    By orcinus in forum Programming
    Replies: 3
    Last Post: 02-09-2011, 11:44 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