【问题标题】:Sort Merged Cells (Excel) - Adapt Code to sort on different column对合并的单元格进行排序 (Excel) - 调整代码以在不同的列上排序
【发布时间】:2016-12-29 17:20:06
【问题描述】:

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

(我的合并单元格) ABCD | E | F |生长激素

我找到了对我的问题的出色响应,但我需要更改原始响应中的列 ([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

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

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

感谢您的帮助! 布伦特

【问题讨论】:

    标签: excel vba


    【解决方案1】:

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

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

    还有

     orgRange = Cells(rowIndex, colStartIndex + 5)
    

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2011-11-24
      • 2016-12-18
      • 2013-05-23
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多