2011-09-26 74 views
1

我有一個相當簡單的Excel文件,主要是佈局(這是我寫的報告),但在文檔中間(第28行),我有一個包含合併單元格的表格。排序包含合併單元格的Excel表格

即A | B | D | E | F

如下:

A | BCD | E | ˚F

同樣是在三行下方,其包含的數據完成的,如下所示:

細胞B28:D28被合併

細胞B29:D29被合併

細胞B30 :D30被合併

細胞B31:D31被合併

當我選擇範圍A28:F31我不能排序任何列,誤差如下:

「此操作需要合併單元格到具有相同大小」

微軟的迴應很簡單,我需要確保我的細胞進行合併。

http://support.microsoft.com/kb/813974

有什麼建議?除了沒有合併細胞?我知道我可以爲細胞居中選擇,但爲了本報告的目的,我需要使用合併的細胞。

回答

2

你不能只複製合併的單元格和「粘貼爲值」到一個單元格?更直接的,爲什麼你必須在這份報告中使用合併的單元格?我個人想不出任何我曾經做過的報告,只是不能以幾種不同的方式重做。

如果您可以儘可能多地告訴我們關於報表(字段,數據類型)的佈局,或者只是發佈截圖,那將會很有幫助。

除非有人有我以前從未見過的東西,不能將整個表格複製到VBA中的數組中,並使用排序算法,否則您將不得不尋找一種方法來合併這些單元格。

再次,舉一個佈局示例,我們將從那裏開始。

+0

好的,這是從一些MySQL表導出的報告..但客戶需要兩個表在同一電子表格(因爲他)。 第一個表(第2-25行)是10列寬。 第二個表格將是第28-35行,但只有8列(上述兩列將被刪除,並且在此表中將給予描述列的附加寬度)。是的,它可以用不均勻的色譜柱完成,但不幸的是需要進行印刷。 一個粗略的例子在這裏: http://coigroup.com/sortmerged.xlsx –

+0

這仍然沒有幫助我把這件事很好地描繪出來。很明顯,MySQL沒有合併信息。您可以輕鬆地在同一電子表格中放置兩個表格。但爲什麼這些細胞需要合併?你有沒有嘗試過使用CONCATENATE功能? – TheFuzzyGiggler

+0

你檢查了這個文件嗎? 忘記我使用MySQL,它可能不會成爲這個問題的原因..因爲我可以複製它創建一個如上所述的文件。 簡單的事實是,在像上面鏈接的文檔中,我無法對第二張表進行排序..但我原以爲這是可能的。我可以過濾它,但即使是過濾器 - >排序失敗。 –

1

這裏是我的答案排序包含非相同的合併細胞的Excel範圍。 這裏按照你的問題的規定,我們需要對'A'進行排序,因此其他的排列如B,C,D ..將被排序。 這裏我指定了一個範圍,其中數據存在於excel「SortRangeValue」中,基本概念是 在'A'上運行氣泡排序,如果我們發現要交換的值只是交換整行(包括合併行和單行行) 在給定的範圍內,我最後有一個隱藏行,這就是爲什麼我運行我的代碼,直到lastRow-3,這裏3代表隱藏行1,1爲長度爲1的泡沫排序和1爲0在Excel基礎索引。(1 + 1 + 1 = 3),並在此它需要一些時間來執行,因爲沒有行的增加

Private Sub Ascending_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 
    Application.ScreenUpdating = False 'dont update screen until we sort the range. 
    Set myRange = Range("SortRangeValue") 'Get the range 
    ' 
    'get the columns and rows from where the row start, since Range can start from any cell 
    ' also the no. of columns and rows in rows 
    rowStartIndex = myRange.Row 
    colStartIndex = myRange.Column 
    colCount = myRange.Columns.Count 
    rowCount = myRange.Rows.Count 
    Dim tempCal As Long 
    tempCal = rowCount + rowStartIndex   ' get the row no of last range 
    'here colStartIndex is the very first column which is to be sorted 
    For iIndex = 0 To rowCount - 3 Step 1  ' Run a bubble sort loop 
     For jIndex = 0 To rowCount - iIndex - 3 Step 1 
      rowIndex = jIndex + rowStartIndex 
      current = Cells(rowIndex, colStartIndex)  ' calculate the rowIndex 
      currentPlusOne = Cells(rowIndex + 1, colStartIndex) ' get current cell value, and next row cell value. 
      If current > currentPlusOne Then  'campair the values 
       ' if match found, select entire row of the values and shift it m down by copying it to some temp location here it is (3,16) 
       'get entire row from firstCol(colStartIndex) to last column and copy it temp location (colStartIndex+colCount-1) 
       Range(Cells(rowIndex + 1, colStartIndex), Cells(rowIndex + 1, colStartIndex + colCount - 1)).Copy Destination:=Cells(3, 16) 
       Range(Cells(rowIndex, colStartIndex), Cells(rowIndex, colStartIndex + colCount - 1)).Copy Destination:=Cells(rowIndex + 1, colStartIndex) 
       Range(Cells(3, 16), Cells(3, 16 + colCount - 1)).Copy Destination:=Cells(rowIndex, colStartIndex) 
       Range(Cells(3, 16), Cells(3, 16 + colCount - 1)).Value = "" 
      End If 
      Next jIndex ' increment jINdex 
     Next iIndex  'Increment iIndex 
     Application.ScreenUpdating = True  'display result on screen 
    End Sub 
0

下面是另一種解決方案從而減少時間從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 
+0

謝謝@rayryeng –

0

使用Google表格。排序和過濾器的工作方式完全相同,但當你想這樣做時它不會給你一個錯誤。