export to excel and order

albertodfa
albertodfa Member Posts: 15
I have created a program to export to excel and at the end i have written:
xlSheet.Range('a6:ad600').Select;
(to select the lines and columns to order)

Question:
now as I can say that I want is orderly for the column B and in ascending order?

Thanks and yours Sincerely

Comments

  • fb
    fb Member Posts: 246
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('b6'));
    
    Reference:
    http://msdn.microsoft.com/library/defau ... 076391.asp
  • albertodfa
    albertodfa Member Posts: 15
    If I wanted to select the sheet on more columns, besides the column B also the column A (in this order), as should I write the code?

    And excuse if I take advantage, if I wanted some subtotals to every variation of A. and to write the total ones under the columns G and H as you must be writes the code?

    I have seen the page that you have attached but I have not succeeded in writing a code what it works.

    Hi and thanks even if you don't answer. :)
  • fb
    fb Member Posts: 246
    There's something broken about the syntax for sorting by multiple columns at the same time:

    http://support.microsoft.com/default.as ... -us;229107

    However, there is a trick that may work, implied by this reference:

    http://support.microsoft.com/default.as ... -us;268007

    This suggests that you achieve a multi-column sorting one column at a time, provided you sort the range starting with the 'least significant' column, and progressing to the 'most significant.'

    In your example then, you would do the following:
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('a6'));
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('b6'));
    

    As for sub-totals, there is a 'SubTotal' method that applies to a Range, but I haven't figured out how to call it from C/AL (yet...!). The SubTotal method requires three parameters -- the first and second are integers, but the third is an Array (COM type = SAFEARRAY). Unfortunately, C/AL does not support the conversion of C/AL arrays into COM SAFEARRAYs, so there is no easy way to provide this third required parameter.

    When C/AL fails, the usual solution is to create a macro in the spreadsheet that will do the work. Then, from C/AL you can invoke the macro using xlApp.Run('MyMacroName');
  • albertodfa
    albertodfa Member Posts: 15
    :) Thanks, I have tried as the example that you have written:

    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('b6'));
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('a6'));

    but so doing, it orders for the column "A" not maintaining the order of the column "B."
    It serves that the relationship among the columns "A" and "B" both:
    Order column "B" and order column "A"

    Hi and thanks again
  • fb
    fb Member Posts: 246
    Let me see if I understand your problem:

    You have created a sheet with values in the range a6:ad600. You want to sort the rows in that range, first by column B, then by column A. If you were using the Excel user interface, you would first highlight the entire range a6:ad600, then click on the Excel menu item 'Data, Sort...'. In the Sort dialog box, in the 'Sort by' box, you would select 'Column B', and in the 'Then by' box, you would select 'Column A'.

    If you do this using the Excel user interface, do you get the results you want?

    If that is correct, then you can get the same result by sorting the range twice:
    • First, sort by column A (only)
    • Then, sort by column B (notice that you have to do the 'separate' sorts in the reverse order)
    That is why I recommended the following code:
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('a6')); 
    xlSheet.Range('a6:ad600').Sort(xlSheet.Range('b6'));
    
    Look at my suggestion carefully -- first column A, then column B -- I know this seems 'backwards', but I'm pretty sure it gets the job done.
  • albertodfa
    albertodfa Member Posts: 15
    :D it was really this way. thanks