0

现在宏执行以下操作: 1. 选择整行。2. 横向排序(从左到右)。3. 转到下一行。(并且准备好让我执行 Ctrl+s 并让它在下一行再次运行。这正是我想要避免的,因为我必须执行 14000 行)。

如何修改代码以从第 1 行一直到第 14000 行执行相同的操作?(如果有一些内容,我不希望它逐行检查,因为这会减慢代码速度。我宁愿现在将行号硬编码为 14000,然后根据需要手动更改)。


Sub SortingOneByOne()
'
' SortingOneByOne Macro
'
' Keyboard Shortcut: Ctrl+s
'
    ActiveCell.Rows("1:1").EntireRow.Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=ActiveCell.Range _
        ("A1:AEA1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange ActiveCell.Range("1:1")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
End Sub

我发现这段代码似乎就是这样做的,但我不知道如何让这两个代码一起工作:

Sub FindEmptyCell_2() 
Dim i As Integer 
Do 
i = i + 1 
Loop Until IsEmpty(Range("A1").Offset(i, 0).Value) 
Range("A1").Offset(i, 0).Select 
End Sub 
4

2 回答 2

0

To modify your code as little as possible you can just use this:

Sub SortingOneByOne()
'
' SortingOneByOne Macro
'
' Keyboard Shortcut: Ctrl+s
'

 For x = 1 to 14000 'From Row 1 to 14000
    ActiveCell.Rows(x).EntireRow.Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=ActiveCell.Range _
        ("A" & x & ":AEA" & x), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange ActiveCell.Range(x)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
Next x
End Sub
于 2013-11-01T17:01:16.613 回答
0

就像是:

Sub Macro1()
    Dim sht As Worksheet, rw As Range

    Set sht = ActiveWorkbook.Worksheets("Sheet1")

    For Each rw In sht.UsedRange.Rows
        With sht.Sort
            .SortFields.Clear
            .SortFields.Add Key:=rw, SortOn:=xlSortOnValues, _
                            Order:=xlDescending, DataOption:=xlSortNormal
            .SetRange rw
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    Next rw
End Sub
于 2013-11-01T16:12:44.450 回答