2011-09-26 97 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表格。排序和过滤器的工作方式完全相同,但当你想这样做时它不会给你一个错误。