宏以基于高,中,低文本突出显示行

时间:2018-06-19 10:49:09

标签: excel excel-vba sorting formatting conditional vba

I have this data

I want to first hightlight all the Cells like this based on the text High(red color), Medium (Orange color and low(Green color)

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

2 个答案:

答案 0 :(得分:0)

Can't you simply:

1) Create three formulas under conditional formatting?

  • Select Columns A-C
  • Open conditional formatting and use formula (x3)
  • =$C1="High" (use Red as the fill color)
  • =$C1="Medium" (use Orange as the fill color)
  • =$C1="Low" (use Green as the fill color)

2) Second step would be to sort your data first on column C, then column A like:

  • Select your data range e.g. Column A-C.
  • Open the sort menu
  • First level: Sort on column C by color, Red color top!
  • Second level: Sort on column C by color, Green color bottom!
  • Third level: Sort on column A by value

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

基本上是:

  1. 确定您在A列中使用的最后一行(因此 知道桌子有多大。)
  2. 清除表格中的所有突出显示 “重新开始”。
  3. 遍历行,直到列中的风险 C为空白(大概在表的末尾。
  4. 根据风险突出显示行。
  5. 所有行完成后,由于风险“ 1”为“高”,因此按列“ D”以升序排序。

我通过记录自己的工作来编写很多此类代码,如其他人在这里建议的那样,然后只是编辑了一些单元格引用以使其“健壮”供您使用。

我希望这会有所帮助!

保罗