Office中国论坛/Access中国论坛

标题: 橫向排序求助 [打印本页]

作者: yjq2006    时间: 2006-11-18 22:56
标题: 橫向排序求助
excel里如何實現橫向由小到大排序?

請大俠編程實現!



謝謝!
作者: guzhen9315    时间: 2006-11-21 09:03
Sub 宏1()

' 橫向由小到大排序


    Range("A1:J1").Select
   
    Selection.Copy
   
    Range("AA1").Select
   
    Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
        , Transpose:=True
   
    Application.CutCopyMode = False
   
    Selection.Sort Key1:=Range("AA1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
        :=xlPinYin
   
    Selection.Copy
   
    Range("A1").Select
   
    Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
             , Transpose:=True
   
    Range("AA1:AA20").Select
   
    Application.CutCopyMode = False
   
    Selection.ClearContents

End Sub




不知道,行不行?

橫向数据如果在 A1:J1,共十个;

列AA,是空闲的列.
作者: 方漠    时间: 2006-11-21 19:08
对当前光标所在的行进行横向排序。

Sub SortByRow()
'**********************
'  2006-11-21 Aaron
'**********************
Dim Ld(1 To 255) As Variant, I, J As Integer
Dim Mvalue As Variant, MI As Integer, Crow As Long, Ccol As Integer

Crow = ActiveCell.Row                            'Get current row no.
Ccol = Cells(Crow, 255).End(xlToLeft).Column     'Get urrent row range last column no.

For I = 1 To Ccol - 1
    Mvalue = Cells(Crow, I).Value                     'Save first cell value to variant
    MI = I                                            'Save column no. to variant
   For J = I + 1 To Ccol
      If Cells(Crow, J).Value < Mvalue Then            'if latter cell value less than var Mvalue, replace it
         Mvalue = Cells(Crow, J).Value
         MI = J
       End If
   Next J
   Cells(Crow, MI).Value = Cells(Crow, I).Value       'exchange the minimal cells' value
   Cells(Crow, I).Value = Mvalue
Next I

If Len(Cells(Crow, 1)) = 0 Then
   Range(Cells(Crow, 1), Cells(Crow, Cells(Crow, 1).End(xlToRight).Column - 1)).Delete Shift:=xlToLeft   'Delete blank cell in head column
End If

End Sub




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3