简体   繁体   English

我如何使用vb6对excel中降序的列进行排序

[英]how can i sort columns descending in excel using vb6

i'm developing a application that load data from DB to an excel sheet. 我正在开发一个应用程序,将数据从数据库加载到Excel工作表。 And in one of these columns, I need to sort in a DESCENDING form. 在这些列之一中,我需要以DESCENDING形式排序。 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: 使用实际值而不是Excel定义的xlXXXX常量:

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

End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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