07-27-2009 01:47 PM
Hello Everyone,
I need to find a way to open and excel document, add a line of data, then sort the data by the first column and resave the document. I have seen and used other activex commands for excel, but I have not found a way to perform what I would like to do. I am also using labview 6.1. I found an example in 7.0 but i could not use it.
Thanks for any help. It has been a nightmare trying to figure out the sort command.
Thanks Jeff
Solved! Go to Solution.
07-27-2009 04:47 PM
Usually the best way to figure out how to use the Excel object model and functions is to have Excel write the macro for you, and then look at the macro code. For example, if you have Excel write a macro where you select the first column and sort it you'll get this:
Columns("A:A").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Basically the important line is the second one, which tells you how to wire up the "Sort" method for a range object:
07-28-2009 08:27 AM
I had created the macro before, but did not know how to handle xlAscending ....ETC.....
I put them in a ring box according to your picture. Attached is the vi. It does not sort yet, but I can assume that you would be able to see what i have done incorrectly. The section you are looking for is at he bottom of the main screen and the bottom left of the diagram.
Thanks Jeff
07-28-2009 09:02 AM
You can't just create arbitrary constants for those Excel constants. You have to right-click on the parameter and select Create->Constant. This will create the appropriate datatype.
As for your code... Um... errr... um.... gee... I'm not even sure where to begin. If you were working for me I'd probably consider firing you. I'm not kidding. Your code is a mess:
Sorry to be so blunt, but sometimes people need to hear the hard cold truth.
I would strongly recommend spending some time reading over some of the design framework patterns documents that are available in the NI Knowledgebase. E.g., state machines and producer-consumer.
Also, this:
is a no-op.
07-28-2009 09:36 AM
Thanks for being blunt. I have used labview enough to understand how to get what i want, but i am not good at grammatics and redundant functions. I am glad it is not my job, as it is just one of the hats i wear and it is a project for the shop floor. It does work the way you see it. I will review the documents that you sent me and i will clean the code up, but the activex for sort does not work. It will open the excel document and highlight everything, not sort, then save and close.
I just dont understand activex well enough make it work on my own.
Thanks for your help
Jeff
07-28-2009 10:08 AM
07-28-2009 11:09 AM
jbreefer wrote:but the activex for sort does not work. It will open the excel document and highlight everything, not sort, then save and close.
I am also seeing this. Quite peculiar. One problem is that when the "Key1" parameter is specified by a string then the assumption is that there's a defined range in the workbook called "A1" (the example in this case). Even if you explicitly wire a range to the "Key1" parameter, as the documentation for Sort indicates, it still doesn't work. Yet it works perfectly well in the macro. I'm not sure what's going on and will need to look at this further.
A short-term fix is to have a macro in the spreadsheet to perform the sort and then simply call the macro from LabVIEW.
07-28-2009 11:13 AM
A short-term fix is to have a macro in the spreadsheet to perform the sort and then simply call the macro from LabVIEW.
This is by far the fastes, easiest and most convenient way.
07-28-2009 11:22 AM
I'll clean the code up and use a macro. thanks for all your help
Jeff