Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sorting in Excel
Message
From
30/01/2004 05:27:58
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
29/01/2004 17:11:59
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00871887
Message ID:
00872218
Views:
27
>Thanks Tamar. As always you've been a great help.
>
>I was able to get the following code to work.
>
>xlDescending = 2
>oSheet.Range("A6:W155").Sort(oSheet.Range("D6"),xlDescending)
>
>but the actual task is a little more complicated. Actually I want to sort on 2 columns (both numeric) and the seemingly obvious line:
>
>oSheet.Range("A6:W155").Sort(oSheet.Range("D6"),xlDescending,;
>oSheet.range("E6"),xlDescending)
>
>does not work.
>
>The macro code is:
>
>Selection.Sort Key1:=Range("D6"), Order1:=xlDescending, Key2:=Range("E6") _
> , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1,MatchCase:=False, _
> Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
> xlSortNormal
>
>but I wasn't able to get it to work even by substituting the additional parameters as in:
>
>oSheet.Range("A6:W155").Sort(oSheet.Range("D6"),xlDescending,oSheet.range("E6"),xlDescending,2,1,"False",1,0,0)
>
>Is there an obvious issue here that I'm missing?
>
>On the other hand perhaps it would be easier to sort the records before I send them to excel. The problem is FoxPro sorts on the sum of the two fields. If I convert the numbers to strings I get a funky result because of the way multi diget numeric strings sort. Got any more ideas?
>
>Thanks again.

False is not a char parameter as "False". To pass false/true either use .F./.T. or 0/1.
Second your parameters are wrong. After second range should be Type which you would omit in case it's not a Pivot. Cehck right syntax in VBA help.
ie: Below code does sorting and subtotalling.
Select Order_id, Cust_ID, Emp_ID, ;
  Order_Date, Order_amt, Order_dsc/100 As Order_dsc,Freight ;
  from orders ;
  into Cursor crsTemp

lcXLSFile = Sys(5)+Curdir()+'myOrders.xls'
Copy To (lcXLSFile) Type Xls


#include "xlConstants.h"
Dimension laArray[3]
laArray[1] = 5 && Order_amt
laArray[2] = 7 && Freight
laArray[3] = Fcount()+1 && Will use later

Use In 'crsTemp'
Use In 'Orders'

oExcel = Createobject("excel.application")
With oExcel
  .Workbooks.Open(lcXLSFile)
  With .ActiveWorkbook.ActiveSheet
    lnRows = .UsedRange.Rows.Count && Get current row count
    lcFirstUnusedColumn = _GetChar(laArray[3]) && Get column in Excel A1 notation
    * Instead of orders order_net field use Excel calculation for net prices
    .Range(lcFirstUnusedColumn+'2:'+;
      lcFirstUnusedColumn+Transform(lnRows)).FormulaR1C1 = ;
      "=RC[-3]*(1-RC[-2])+RC[-1]"
    .Range(lcFirstUnusedColumn+'1').Value = 'Order Net' && Place header
    .Range('E:'+lcFirstUnusedColumn).NumberFormat = "$#,##0.0000" && Format columns
    .Range('F:F').NumberFormat = "0%"

    * Sort : Cust_Id desc, Emp_ID desc, Order_Amt ascending
    .UsedRange.Sort(.Range('B2'),xlDescending,.Range('C2'),,xlDescending,;
      .Range('E2'),,xlGuess,,.F.,xlTopToBottom)

    * Subtotal grouping by customer
    .UsedRange.Subtotal(2, xlSum, @laArray, 1, 0, 1)
    .UsedRange.Columns.Autofit && Autofit columns
  Endwith
  .Visible = .T.
Endwith

* Return A, AA, BC etc noation for nth column
Function _GetChar
  Lparameters tnColumn && Convert tnvalue to Excel alpha notation
  If tnColumn = 0
    Return ""
  Endif
  If tnColumn <= 26
    Return Chr(Asc("A")-1+tnColumn)
  Else
    Return _GetChar(Int(Iif(tnColumn % 26 = 0,tnColumn - 1, tnColumn) / 26)) + ;
      _GetChar((tnColumn-1)%26+1)
  Endif
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform