Link to home
Start Free TrialLog in
Avatar of toooki
toooki

asked on

Excel column ordering automatically

I created an web application that let users download the application content in an excel file (csv file). In the application I have no ways to pre-define the column order in the downloaded excel file (csv file).

The downloaded excel file comes in incorrect yet fixed field order (say F5  F1  F4  F2  F3 F8 F7 F6). Is there any way (using excel macro or some other I do not know much) that will cause the excel file to show the correct order of the columns when the excel file is opened? On top of that is it possible for the excel file to show only the first 5 fields like this:
F1 F2 F3 F4 F5

The field names are not actually F1, F2 etc. but the field names are known (those to be included and excluded).

Thanks a lot.
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Excel will open the file in the order it is in in the file as it is simply a csv.

You can order it in excel easily using macro, but the csv will not have macro code.

How are you creating the csv?

You are more likely going to be able to create the csv in the format you need by changing a query or similar than get a csv to reorder itself.
Avatar of toooki
toooki

ASKER

Thank you.
In my application when I click a tab or link, it downloads the file to the default download folder as a mydownload.csv file. If I am using chrome browser, it shows up at the bottom of the browser. If in firefox, it shows in the top arrow link in the browser.

I have no choice to choose the format in the application side. It is a known problem and I talked with the application vendor ..

So when I open the mydownload.csv file with excel, where do I write the excel macro code to reorder the fields and also is there any example of such macro code..?
Thank you.
SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toooki

ASKER

Thank you. I have attached a sample file with a few records. (File name Sample1.xlsx). Where shall I put the VB code that you mentioned above.
Thank you.
tooki.. you have not attached the sample file... been there done that soo many times...

to use the code above... hit [ALT]+[F11] to open VBA editor.
use INSERT > MODULE
then paste the code into the white space that should now open up in the main area.
Avatar of toooki

ASKER

I apologize. I attached the sample file. I added the code the way you mentioned. But what do I do next. Is it now supposed to alter the column order?
Thanks a lot.Sample1.xlsm
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial