简体   繁体   中英

how can i sort columns descending in excel using vb6

i'm developing a application that load data from DB to an excel sheet. And in one of these columns, I need to sort in a DESCENDING form. But I just cant do this, because all the time this give me errors... In the ascending form (default) I can do it and it passes without any trouble, but when I try to put the parameter of the descending, this don't pass it.

EDIT* First, declarations and constants:

Dim obj_excel As Object
Set obj_excel = CreateObject("Excel.Application")
Dim oSheet As Object ' Worksheet
Dim oChart As Object ' To declare chart Excel

obj_excel.Workbooks.Add 'add a workbook to the app
obj_excel.Sheets(w_Plan1).Select
obj_excel.Sheets("Folha2").Name = "Provider"

obj_excel.cells(1, w_coluna).Font.Bold = True

'header
obj_excel.cells(1, w_coluna).Font.Size = 10
obj_excel.cells(1, w_coluna).Value = "OF"
obj_excel.cells(1, w_coluna).HorizontalAlignment = -4108

'Assigning values to one cell
obj_excel.cells(w_linha, 2).Font.Bold = False
obj_excel.cells(w_linha, 2).Font.Size = 10
obj_excel.cells(w_linha, 2).Value = obj_cmpcil0.H_cdforneced1
obj_excel.cells(w_linha, 2).HorizontalAlignment = -4108
...
.....
......
'Creating a chart
Set oSheet = obj_excel.worksheets.Item(2)
Set oChart = oSheet.ChartObjects.Add(250, 10, 660, 450).Chart
oChart.SetSourceData Source:=oSheet.Range(w_Plan2 & "!$A$1:$C$11")

Of course there is a lot more code... but I just put here fragments of code and the chart built like example to you see how my code is organized and declared..

*FINISH EDIT

the ascending form that my code works:

    obj_excel.Sheets(2).Range("A2:C25").Sort _
        key1:=obj_excel.Sheets(2).Columns("C")

the form that I tryng to add the descending Parameter:

obj_excel.Sheets(2).Range("A2:C25").Sort _
        key1:=obj_excel.Sheets(2).Columns("C") _
        Order1:=xlDescending, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal

the parameters of method sort, isnt it all optional?

Using actual values instead of Excel-defined xlXXXX constants:

With obj_excel.Sheets(2)
    .Range("A2:C25").Sort key1:=.Columns("C"), _
                    Order1:=2, _
                    OrderCustom:=1, _
                    MatchCase:=False, _
                    Orientation:=1, _
                    DataOption1:=0

End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM