Then I want to sort it like this based on column c and a
我们如何为此编写宏?
经过一些建议后编辑: 我尝试了条件格式化的录制技巧,我能够得到结果。只缺少一部分;我无法排列High> Medium> Low的序列 它总是按字母顺序排列表格,即高>低>介质
Sub ST03N()
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Functional Heat Map"
Worksheets("Functional Heat Map").[A:B].Value = Worksheets("Detailed Analysis").[E:F].Value
Worksheets("Detailed Analysis").Range("N:N").Copy Destination:=Worksheets("Functional Heat Map").Range("C:C")
Columns("A:C").Select
Selection.EntireColumn.AutoFit
With ActiveSheet.Sort
.SortFields.Add Key:=Range("A1"), Order:=xlAscending
.SortFields.Add Key:=Range("B1"), Order:=xlAscending
.SetRange Range("A:C")
.Header = xlYes
.Apply
End With
Cells.RemoveDuplicates Columns:=Array(2)
With ActiveSheet
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("C:C"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
CustomOrder:="High"
.Sort.SortFields.Add Key:=.Range("C:C"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
CustomOrder:="Medium"
.Sort.SortFields.Add Key:=.Range("G:G"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
CustomOrder:="Low"
.Sort.SetRange .Range("A:AA")
.Sort.Header = xlYes
.Sort.MatchCase = False
.Sort.Apply
End With
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:C" & Lastrow).Select
Range("C2").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$C2=""High"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$C2=""Medium"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ColorIndex = 44
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$C2=""Low"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
ActiveSheet.[A1:C1].Font.Bold = True
End Sub
答案 0 :(得分:0)
Can't you simply:
1) Create three formulas under conditional formatting?
2) Second step would be to sort your data first on column C, then column A like:
If you want to create VBA macro's I think you should leave part 1 to Excel with conditional formatting. The second part you could simply record when you try this manually (you might need to adjust ranges)
答案 1 :(得分:0)
正如其他一些人所说的那样,对于bandwagon毫无意义,您本可以自己发布一些代码。我的尝试看起来像这样:
我添加了一个列“ D”,该列使用此公式将风险归类为数字(在D2单元格中并向下复制):
= IF(C2 =“ High”,1,IF(C2 =“ Medium”,2,IF(C2 =“ Low”,3,0)))
我认为这对帮助排序很重要,因为很难对单词High / Medium / Low进行排序并使它们按所需顺序出现。如果您不希望用户看到这些数字,则可以使用自定义格式(“ ;;;”)设置单元格的格式,该格式将隐藏单元格的值或隐藏列等。
假设所有数据都在“ sheet1”中,则以下代码可能有效。它不是有史以来最精巧的代码,但是它似乎对我有用:
Sub dohighlightandsort()
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet1").Range("A2:D" & lastrow).Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
readrow = 2
currentrisk = Worksheets("Sheet1").Range("C" & readrow).Value
Do Until currentrisk = ""
If currentrisk = "High" Then
thiscolour = 255 ' lovely red colour
ElseIf currentrisk = "Medium" Then
thiscolour = 65535 ' lovely yellow colour
ElseIf currentrisk = "Low" Then
thiscolour = 5296274 ' very lovely green colour
End If
Worksheets("Sheet1").Range("A" & readrow & ":C" & readrow).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = thiscolour
.TintAndShade = 0
.PatternTintAndShade = 0
End With
readrow = readrow + 1
currentrisk = Worksheets("Sheet1").Range("C" & readrow).Value
Loop
' Now that the loop is done, sort the table by risk.
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2:D17"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:D17")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
基本上是:
我通过记录自己的工作来编写很多此类代码,如其他人在这里建议的那样,然后只是编辑了一些单元格引用以使其“健壮”供您使用。
我希望这会有所帮助!
保罗