我有一个相当简单的Excel文件,主要是布局(这是我写的报告),但是在文档的中间(第28行),我有一个包含合并单元格的表。
即。 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
有什么建议吗?除了未合并的细胞?我知道我可以为选择中心选择单元格,但出于本报告的目的,我需要使用合并单元格。
答案 0 :(得分:2)
您不能只将复合的单元格和“粘贴为值”复制到单个单元格中吗?更直接的是,您为什么要在此报告中使用合并单元格?我个人无法想到我曾经做过的任何报告,不能以几种不同的方式重做。
如果你能尽可能地告诉我们关于报告的布局(字段,数据类型),或者只是发布截图,它会有很多帮助。
除非有人拥有我以前从未见过的东西,否则如果没有将整个表复制到VBA中的数组并使用排序算法,那么你将不得不找到一种方法来合并这些小区。
再次,举一个例子作为布局,我们将从那里开始。
答案 1 :(得分:1)
这是我对包含非相同合并单元格的excel范围进行排序的答案。 在您的问题中指定的地方,我们需要对'A'进行排序,因此其他的col,即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
答案 2 :(得分:0)
这是另一种解决方案,可将执行时间从30秒缩短到小于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
答案 3 :(得分:0)
使用Google表格。排序和过滤器的工作方式完全相同,但是当您想要这样做时,它不会给您一个错误。