Solved

Export to excel from access table loses sort order

Posted on 2014-12-01
12
830 Views
Last Modified: 2014-12-10
I have a table that I need to export to excel.   Issue is that when I create a macro to export from the table it loses the sort order.  Trying to export with formatting replaces my existing spreadsheet rather than updating it.

What are my options with exporting from a query?  I do not have the same options available if I choose to export from a query instead of a table using the importexportspreadsheet option.
0
Comment
Question by:gracie1972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40474903
I would pull this from the Excel end, by defining an external datasource that points to the Acccess database and contains a SELECT statement with and ORDER BY clause.

This link https://support.office.com/en-gb/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e?ui=en-US&rs=en-GB&ad=GBMS Office 2010  help link
You can then choose whether to refresh the query manually or automatically every time the workbook opens.
0
 

Author Comment

by:gracie1972
ID: 40474909
Issue is it is a shared workbook we tried that before and it kept locking up.
0
 
LVL 18

Expert Comment

by:Simon
ID: 40474926
OK. Is it a possibility to turn off the sharing while you refresh the data?

Can you tell us a bit more about your scenario and why the workbook is being shared. I don't use shared workbooks normally because of the restrictions

See also previous EE thread on limitations of shared workbooks
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:gracie1972
ID: 40474940
The shared workbook is to account for the fact if someone has it open while the query is trying to refresh the data on the backend.  We work in a large company and people leave files open all the time.  Trying to enforce this is a problem, if the workbook is open we can use the importexportspreadsheet feature which has solved this problem.

Now it just won't sort correctly.
0
 
LVL 34

Expert Comment

by:Norie
ID: 40474947
Can you post the macro/code that you are using to export to Excel?
0
 
LVL 18

Expert Comment

by:Simon
ID: 40474954
Perhaps you could run the query from a second (unshared) workbook and then paste the whole sheet into the shared workbook.  I can't test this from where I'm sitting, but it would potentially mean you could get the data sorted and formatted as you want, and could run the operation from a button in the unshared workbook.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40474980
@SimonAdept

Or we can suggest CopyFromRecordset :) Although
@gracie1972
"Trying to export with formatting replaces my existing spreadsheet rather than updating it."

Now I am confused.
Can you explain, in detail, what you are doing from where and post your code

Nick67
0
 

Author Comment

by:gracie1972
ID: 40474990
I actually do not have any code or I would post it.  
So far all I do is run a macro (see attached picture).  

This macro exports data from a table to a specific tab within an open or closed workbook.  This process seems to work whether a user has the file open or not.

The issue is when it exports that data it loses the sort order from the table.  I

Is it better to write a function or code to do this?
Capture.JPG
0
 
LVL 18

Expert Comment

by:Simon
ID: 40474991
@Nick, I'm not sure exactly what limitations there are in the shared workbook - I thought of CopyFromRecordset (honest, I did!) but hesitated to suggest it. I also just read some stuff on SO that suggested it didn't work in shared workbooks, but you may know better...
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40475007
ImportExportSpreadsheet Macro Action
http://msdn.microsoft.com/en-us/library/office/ff193927(v=office.15).aspx
The name of the Access table to import spreadsheet data to, export spreadsheet data from, or link spreadsheet data to. You can also type the name of the Access select query you want to export data from.

Perhaps...ding, ding, ding ... we'll have a winner.
Your image doesn't show what you are exporting.
Is it a query or table?
If it is NOT a query, can you create a query with an Order By clause that will be the way you want the data sorted?
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40475010
It looks like you can create a query that is
Select * from Stores Order By  and then whatever sort order you need.
One guesses
Select * from Stores Order By StoreName;
Save it as qryExportStores

In your macro, change Table from Stores to qryExportStores and have a go at it
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40475701
> when I create a macro to export from the table it loses the sort order.

That's because a table has no sort order.
Create a query as Nick explains and export that.

/gustav
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query Dilema in Access 2010 3 36
managing events in  the webrowser control 14 66
Handle Apostrophes in VBA SQL - Part 2 10 46
Why can't I change data in my query? 3 35
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question