![](/img/trans.png)
[英]How to delete rows in excel if the cell in the first column isn't bold?
[英]How to Sort Excel Column S.T. Rows w/ Null/Empty Column Cell Go On Top?
运行宏时,我有几行具有数据和“标志”的行,这些行与数据相邻。 例如:
名字 | 姓氏 | 旗
John | Smith | Needs a Bath
Cindy | LuWho |
Bob | Loblaw | Needs a Bath
目标:
我希望行w / o标志(即列C == NULL /空字符串)排序到顶部,然后按B列排序A-> Z得到这个:
辛迪| LuWho |
鲍勃| Loblaw | 需要洗个澡
约翰| 史密斯| 需要洗个澡
我试过的:
使用Excel 2007的“排序”,我完成了排序依据(C列),排序(值)排序依据(A到Z)和(Z到A)。 A到Z和Z到A都会导致顶部而不是底部的标记行。
之前: 后:
我最终想要代码,但我现在正试图弄清楚如何手动完成它,以便我可以通过Excel的“记录宏”获取代码。
根据我上面的评论,问题是您使用的计算结果为空字符串。 如果该字段实际上是空的,那么您将拥有所需的行为。
这是一种肮脏但却有效的方法:
=IF(C2<>"",2,1)
并填写。 我结束了IMO的解决方案,比@Poweruser创建另一个列,填充它,隐藏它,然后在隐藏列上使用sort更优雅。 我的方法使用基于条件格式的字体颜色更改并对其进行排序。
=IF(INDIRECT("RC",0)="",TRUE,FALSE)
通过对录制的宏进行一点修补,我得到了以下工作代码(在对'空'单元格进行排序后,它还按值排序另一列):
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
其中一个解决方案是在排序之前用单引号( '
)替换空格。 它不可见但不是NULL
。 请参阅代码示例:
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.