[英]How to Sort Excel Column S.T. Rows w/ Null/Empty Column Cell Go On Top?
I have several rows that have data and 'flags' that are raised adjacent to the data when a macro is run. 运行宏时,我有几行具有数据和“标志”的行,这些行与数据相邻。 For example: 例如:
First Name | 名字 | Last Name | 姓氏 | Flag 旗
John | Smith | Needs a Bath
Cindy | LuWho |
Bob | Loblaw | Needs a Bath
Goal: 目标:
I want the rows w/o flags (ie where column C == NULL/Empty string) to be sorted to the top and then sort by column B by A->Z to get this: 我希望行w / o标志(即列C == NULL /空字符串)排序到顶部,然后按B列排序A-> Z得到这个:
Cindy | 辛迪| LuWho | LuWho |
Bob | 鲍勃| Loblaw | Loblaw | Needs a Bath 需要洗个澡
John | 约翰| Smith | 史密斯| Needs a Bath 需要洗个澡
What I've Tried: 我试过的:
Using Excel 2007's 'Sort', I've done Sort By (Column C), Sort On (Values) Sort By (A to Z) and (Z to A). 使用Excel 2007的“排序”,我完成了排序依据(C列),排序(值)排序依据(A到Z)和(Z到A)。 Both A to Z and Z to A result in the flagged rows on top, not the bottom. A到Z和Z到A都会导致顶部而不是底部的标记行。
Before: 之前: After: 后:
I ultimately want the code, but I'm currently trying to figure out how to do it by hand so I can then get the code through Excel's 'Record Macro'. 我最终想要代码,但我现在正试图弄清楚如何手动完成它,以便我可以通过Excel的“记录宏”获取代码。
Per my comment above, the problem is that you are using a formula that evaluates to an empty string. 根据我上面的评论,问题是您使用的计算结果为空字符串。 If the field was actually empty, you'd have the behavior you are looking for. 如果该字段实际上是空的,那么您将拥有所需的行为。
Here's a dirty-but-it-works approach: 这是一种肮脏但却有效的方法:
=IF(C2<>"",2,1)
and fill down. 使用公式=IF(C2<>"",2,1)
并填写。 I ended coming up with a solution that IMO, is more elegant than @Poweruser 's creating another column, populating it, hiding it, and then using sort on the hidden column. 我结束了IMO的解决方案,比@Poweruser创建另一个列,填充它,隐藏它,然后在隐藏列上使用sort更优雅。 My method utilizes font color changes based on conditional formatting and sorts off of that. 我的方法使用基于条件格式的字体颜色更改并对其进行排序。
=IF(INDIRECT("RC",0)="",TRUE,FALSE)
使用条件格式>新规则>'使用公式确定要格式化的单元'并在文本框中使用公式=IF(INDIRECT("RC",0)="",TRUE,FALSE)
With a little bit of tinkering of the recorded macro, I got the following working code (which also sorts by value another column after sorting for the 'blank' cells): 通过对录制的宏进行一点修补,我得到了以下工作代码(在对'空'单元格进行排序后,它还按值排序另一列):
For oRow = 2 To iFinalRow
ActiveWorkbook.ActiveSheet.Cells(oRow, 5).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF(INDIRECT(""RC"",0)="""",TRUE,FALSE)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next oRow
'Sort
ActiveWorkbook.ActiveSheet.SORT.SortFields.Clear
ActiveWorkbook.ActiveSheet.SORT.SortFields.Add(Range("E:E"), _
xlSortOnFontColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(31, 73, 125)
ActiveWorkbook.ActiveSheet.SORT.SortFields.Add _
Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.ActiveSheet.SORT
.SetRange Range("A:F")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
One of the solutions is to replace blanks with single quote ( '
) before sorting. 其中一个解决方案是在排序之前用单引号( '
)替换空格。 It is not visible but it is not NULL
. 它不可见但不是NULL
。 See the code example: 请参阅代码示例:
Public Sub Sort_blanks()
Dim lastrow As Integer
' The number of the last row
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
' Replace blanks with with single quote
Range("C2:C" & lastrow).Select
Application.DisplayAlerts = False
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "'"
Application.DisplayAlerts = True
On Error GoTo 0
' Sort
Range("A:C").Sort key1:=Range("C:C"), key2:=Range("B:B"), _
order1:=xlAscending, order2:=xlAscending, Header:=xlYes
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.