排序合并单元格(Excel) - 调整代码以对不同列进行排序

时间:2016-12-29 17:20:06

标签: excel-vba vba excel

我正在尝试对有人已应用合并单元格的范围(基于F列)进行排序。

(我的合并单元格) ABCD | E | F | GH

我找到了对我的问题的出色回应,但我需要更改orignial响应中的列([https://stackoverflow.com/questions/7549570/sorting-an-excel-table-that-contains-merged-cells#=][1]

代码在A列上排序(冒泡排序),我的问题需要在F列上排序(对于另一个范围也是B列,但如果我学习如何在一个地方进行更改,我可以弄清楚如何改变另一个)。

我已多次介绍它,我相信这条线需要在这里进行更改(可能是colStartIndex + 6)

Range(Cells(rowStartIndex, colStartIndex), Cells(tempCal, colStartIndex)).Copy Destination:=Range(Cells(tempRowIndex, tempColIndex), Cells(tempRowIndex + tempCal, tempColIndex))

以及此处(将冒泡排序与临时列表进行比较)

Dim orgRange, tempRange As Long
     For iIndex = 0 To rowCount - 2 Step 1
        rowIndex = iIndex + tempRowIndex
        tempRange = Cells(rowIndex, tempColIndex)
        'MsgBox (tempRange)
            For jIndex = 0 To rowCount - 2 Step 1
                rowIndex = jIndex + rowStartIndex
                orgRange = Cells(rowIndex, colStartIndex)

                If tempRange = orgRange Then
                    'MsgBox ("Match Found : \n (tempRange,orgRange) : (" & tempRange & "," & orgRange & ")")

                   Range(Cells(rowIndex, colStartIndex), Cells(rowIndex, colStartIndex + colCount - 1)).Copy Destination:=Cells(tempFinalRowIndex + iIndex, tempFinalColIndex)
              End If
          Next jIndex
       Next iIndex

这是原始回复中发布的整个子过程。我已经为自己做了一些额外的评论作为学习辅助工具。除了更改排序列之外,我理解所有代码。

Private Sub QuickAscending_Click()
Dim myRange As Range        'variable to hold the Named Range
Dim rowCount As Long        'variable to hold the Number of Rows in myRange
Dim colCount As Long        'variable to hold the Number of Columns in myRange
Dim rowStartIndex As Long   'variable to hold the Starting Row index of myRange
Dim colStartIndex As Long   'variable to hold the Starting Col index of myRange
Dim iIndex As Long          'Variable used for iteration
Dim jIndex As Long          'Variable used for iteration
Dim current As Long         'used in bubble sort to hold the value of the current jIndex item
Dim currentPlusOne As Long          'used in bubble sort to hold the value of the  jIndex+1 item
Dim rowIndex As Long
Dim tempRowIndex, tempColIndex As Long
Application.ScreenUpdating = True
Set myRange = Sheets("Sheet1").Range("SortRangeValue")
rowStartIndex = myRange.Row
colStartIndex = myRange.Column
colCount = myRange.Columns.Count
rowCount = myRange.Rows.Count
Dim tempCal As Long
tempCal = rowCount + rowStartIndex - 1

''''''this sets the process to bubble sort. Copies array (first column) to temp location and then loops to bubble sort
tempRowIndex = 6
tempColIndex = 200
Range(Cells(rowStartIndex, colStartIndex), Cells(tempCal, colStartIndex)).Copy Destination:=Range(Cells(tempRowIndex, tempColIndex), Cells(tempRowIndex + tempCal, tempColIndex))
     For iIndex = 0 To rowCount - 3 Step 1
        For jIndex = 0 To rowCount - iIndex - 3 Step 1
            rowIndex = jIndex + tempRowIndex
            current = Cells(rowIndex, tempColIndex)
            currentPlusOne = Cells(rowIndex + 1, tempColIndex)
            If current > currentPlusOne Then
            Cells(rowIndex, tempColIndex) = currentPlusOne
            Cells(rowIndex + 1, tempColIndex) = current
            End If
       Next jIndex
     Next iIndex

     Dim tempFinalRowIndex, tempFinalColIndex As Long
     tempFinalRowIndex = 6
     tempFinalColIndex = 201

''''''This part compares the bubble sort and copies the row to the temp location
     Dim orgRange, tempRange As Long
     For iIndex = 0 To rowCount - 2 Step 1
        rowIndex = iIndex + tempRowIndex
        tempRange = Cells(rowIndex, tempColIndex)
        'MsgBox (tempRange)
            For jIndex = 0 To rowCount - 2 Step 1
                rowIndex = jIndex + rowStartIndex
                orgRange = Cells(rowIndex, colStartIndex)

                If tempRange = orgRange Then
                    'MsgBox ("Match Found : \n (tempRange,orgRange) : (" & tempRange & "," & orgRange & ")")

                   Range(Cells(rowIndex, colStartIndex), Cells(rowIndex, colStartIndex + colCount - 1)).Copy Destination:=Cells(tempFinalRowIndex + iIndex, tempFinalColIndex)
              End If
          Next jIndex
       Next iIndex

    ''Application.ScreenUpdating = True
    ''''''This copies the temp last back to the original range
    Range(Cells(tempFinalRowIndex, tempFinalColIndex), Cells(tempFinalRowIndex + rowCount - 2, tempFinalColIndex + colCount - 1)).Copy Destination:=Range(Cells(rowStartIndex, colStartIndex), Cells(rowStartIndex + rowCount - 2, colStartIndex + colCount - 1))
    ''''deletes the temp rows
    Range(Cells(tempFinalRowIndex - 1, tempFinalColIndex), Cells(tempFinalRowIndex + rowCount - 2, tempFinalColIndex + colCount - 1)).Delete
    Range(Cells(tempRowIndex, tempColIndex), Cells(tempRowIndex + rowCount - 2, tempColIndex)).Delete
End Sub

感谢您的帮助! 布伦特

1 个答案:

答案 0 :(得分:0)

我能够调整以下代码以使用我需要的列。

Range(Cells(rowStartIndex, colStartIndex + 5), Cells(tempCal, colStartIndex + 5))

以及

 orgRange = Cells(rowIndex, colStartIndex + 5)