下面是另一種解決方案從而減少時間從30秒到執行到JST少於2秒。以前的代碼的問題是它交換行很多次。在這段代碼中,我正在複製'A'列並首先對它進行排序,然後創建一個臨時範圍,在該範圍中,我將保存已排序的整行值(列'A'條目),然後將臨時排序的範圍替換爲原始範圍。
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 = False
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 - 2
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
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
Range(Cells(tempFinalRowIndex, tempFinalColIndex), Cells(tempFinalRowIndex + rowCount - 2, tempFinalColIndex + colCount - 1)).Copy Destination:=Range(Cells(rowStartIndex, colStartIndex), Cells(rowStartIndex + rowCount - 2, colStartIndex + colCount - 1))
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
好的,這是從一些MySQL表導出的報告..但客戶需要兩個表在同一電子表格(因爲他)。 第一個表(第2-25行)是10列寬。 第二個表格將是第28-35行,但只有8列(上述兩列將被刪除,並且在此表中將給予描述列的附加寬度)。是的,它可以用不均勻的色譜柱完成,但不幸的是需要進行印刷。 一個粗略的例子在這裏: http://coigroup.com/sortmerged.xlsx –
這仍然沒有幫助我把這件事很好地描繪出來。很明顯,MySQL沒有合併信息。您可以輕鬆地在同一電子表格中放置兩個表格。但爲什麼這些細胞需要合併?你有沒有嘗試過使用CONCATENATE功能? – TheFuzzyGiggler
你檢查了這個文件嗎? 忘記我使用MySQL,它可能不會成爲這個問題的原因..因爲我可以複製它創建一個如上所述的文件。 簡單的事實是,在像上面鏈接的文檔中,我無法對第二張表進行排序..但我原以爲這是可能的。我可以過濾它,但即使是過濾器 - >排序失敗。 –