2014-10-02 70 views
0

我有一个表中的标题和从A到D的列中的数据随着行号的变化(行数超过66800)。我想从Z到A的数据按列C排列。如何在VBA中进行排序?

在互联网上有很多不同的VBA解决方案,但是没有一个能够为我正确工作。

我的代码给我一个错误:

Sub SortDescending() 
Dim lRow As Long 
Dim lCol As Long 
lRow = Sheets("atm_hh").Cells(Rows.Count, 1).End(xlUp).Row 
lCol = Sheets("atm_hh").Cells(1, Columns.Count).End(xlToLeft).Column 

    With Sheets("atm_hh") 
    .Select 
    .Range("A2:" & Cells(lRow, lCol).Address).Sort Key1:=Range("C2"), _ 
               Order1:=xlDescending, _ 
               Header:=xlGuess, _ 
               OrderCustom:=1, _ 
               MatchCase:=False, _ 
               Orientation:=xlTopToBottom 
    End With 
End Sub 
+0

你得到了什么错误? – 2014-10-02 14:39:18

+0

我的意思不是VBA错误,但在Excel中,C列的排序错误。例如,9,99大于33,45等。 – Ale 2014-10-02 14:42:12

+0

也许excel不能识别您的语言环境中的逗号,并且您需要使用点作为千位分隔符? – 2014-10-02 14:43:31

回答

0

尝试

Sub SortDescending() 

    Dim sh As Worksheet 
    Set sh = Sheets("atm_hh") 

    Dim lRow As Long 
    Dim lCol As Long 
    lRow = sh.Cells(Rows.Count, 1).End(xlUp).Row 
    lCol = sh.Cells(1, Columns.Count).End(xlToLeft).Column 

    Dim cell As Range 
    For Each cell In sh.Range("C2:C" & lRow) 
     cell = WorksheetFunction.Substitute(cell, ",", ".") 
    Next 

    Columns("C:C").NumberFormat = "0.0" 

    sh.Cells.AutoFilter 
    sh.AutoFilter.Sort.SortFields.Clear 
    sh.AutoFilter.Sort.SortFields.Add Key:=Range("C1:C" & lRow), _ 
     SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal 
    With sh.AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    sh.AutoFilterMode = False 
End Sub 
+0

它可以工作,但是对于列(“C:C “).NumberFormat =”0“它将0.55设置为55.我应该使用列(”C:C“)。NumberFormat =”0.0“? – Ale 2014-10-02 15:11:45

+0

如果你想要前面的零是啊 – 2014-10-02 15:14:13

+0

well 55和0.55是不一样的,我需要以正确的方式保存数据,因此我需要总结它们,我需要0.00格式的数据。没关系,我的意思是它不会改变价值吗? – Ale 2014-10-02 15:16:23