Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对包含合并单元格的Excel表进行排序_Excel_Sorting_Merge - Fatal编程技术网

对包含合并单元格的Excel表进行排序

对包含合并单元格的Excel表进行排序,excel,sorting,merge,Excel,Sorting,Merge,我有一个相当简单的Excel文件,主要是布局(这是我写的报告),但在文档的中间(第28行),我有一个包含合并单元格的表 i、 e.A | B | D | e | F 详情如下: A | BCD | E | F 下面包含数据的三行也是如此,如下所示: 单元B28:D28被合并 单元B29:D29被合并 单元格B30:D30被合并 单元格B31:D31被合并 当我选择范围A28:F31时,我无法按任何列排序,错误如下: “此操作要求合并的单元格大小相同” 微软的回应很简单,我需要确保我的单元格被合并

我有一个相当简单的Excel文件,主要是布局(这是我写的报告),但在文档的中间(第28行),我有一个包含合并单元格的表

i、 e.A | B | D | e | F

详情如下:

A | BCD | E | F

下面包含数据的三行也是如此,如下所示:

单元B28:D28被合并

单元B29:D29被合并

单元格B30:D30被合并

单元格B31:D31被合并

当我选择范围A28:F31时,我无法按任何列排序,错误如下:

“此操作要求合并的单元格大小相同”

微软的回应很简单,我需要确保我的单元格被合并


有什么建议吗?除了未融合的细胞?我知道我可以集中选择单元格,但出于本报告的目的,我需要使用合并单元格。

您不能只将合并单元格复制并“粘贴为值”到单个单元格中?更直接地说,为什么必须在此报告中使用合并单元格?我个人认为我所做的任何报告都不能用几种不同的方式重做

如果你能尽可能多地告诉我们报告的布局(字段、数据类型),或者只是发布一个屏幕截图,那会很有帮助

除非有人有我以前从未见过的东西,除了将整个表复制到VBA中的数组并使用排序算法之外,您必须找到一种方法来合并这几个单元格


再次给出一个布局示例,我们将从这里开始。

下面是我对包含不相同合并单元格的excel区域进行排序的答案。 如您问题中所述,我们需要对“A”进行排序,并相应地对其他列进行排序,即B、C、D。。我会整理好的。 在这里,我指定了一个数据存在于excel“SortRangeValue”中的范围,其基本概念是 在“A”上运行冒泡排序,如果发现要交换的值,只需交换整行(包括合并行和分离行) 在给定的范围内,我终于有了一个隐藏行,这就是为什么我一直运行我的代码直到最后一行-3,这里3表示1表示隐藏行,1表示长度-1表示冒泡排序,1表示excel中基于0的索引。(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

下面是另一个解决方案,它将执行时间从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列(上面的两列将被删除,并且该表中的description列将增加宽度)。是的,它可以用不均匀的列来完成,但不幸的是,布局需要进行打印。这里有一个粗略的例子:这仍然不能帮助我很好地理解这一点。显然MySQL没有合并信息。您可以在同一个电子表格中轻松创建两个表格。但是为什么需要合并这些单元呢?你试过使用连接函数吗?你检查过文件了吗?忘了我使用MySQL,它可能不会成为这个问题的原因。。因为我可以复制它,创建一个如上所述的文件。简单的事实是,在我上面链接的文档中,我无法对第二个表进行排序。。但我认为这是可能的。我可以过滤它,但即使过滤->排序也失败。很抱歉,您的原始答复在我的邮件上被切断,我没有看到该文件。为什么不在第二个表中将标题C/D保留为空行?然后,您可以改变格式,但你想好看。或者,您可以将数据保存在一页上,并将另一页中的单元格与打印所需的格式链接起来。您可以关闭“显示网格”(show grid),使其看起来与您现在拥有的完全相同,无论采用哪种方式打印,其外观都相同。除非有比我知道的更多的要求。@Warren请用一个样本更新你上传的例子,说明你的数据在分类后会是什么样的。另外,您使用的是什么版本的excel?