Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 宏以基于高、中、低文本高亮显示行_Excel_Sorting_Formatting_Conditional_Vba - Fatal编程技术网

Excel 宏以基于高、中、低文本高亮显示行

Excel 宏以基于高、中、低文本高亮显示行,excel,sorting,formatting,conditional,vba,Excel,Sorting,Formatting,Conditional,Vba,我们如何为此编写宏 在提出几点建议后编辑: 我尝试了条件格式的录制技巧,我能够得到结果。只有一部分缺失;我无法安排高>中>低的顺序 它总是按字母顺序排列表格,即高>低>中 Sub ST03N() ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count) ActiveSheet.Name = "Functional Heat Map" Worksheets("Functional Heat Map").[A:B].Value =

我们如何为此编写宏

在提出几点建议后编辑: 我尝试了条件格式的录制技巧,我能够得到结果。只有一部分缺失;我无法安排高>中>低的顺序 它总是按字母顺序排列表格,即高>低>中

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

正如其他一些人所说的那样,你本可以自己发布更多的代码,而不想随大流。我的尝试如下所示:

我已经添加了一列“D”,该列使用此公式将风险分类为一个数字(在单元格D2中并向下复制):

=如果(C2=“高”,1,如果(C2=“中”,2,如果(C2=“低”,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
它基本上是:

  • 确定在列A中使用的最后一行(因此 知道桌子有多大)
  • 清除表中的任何突出显示 “重新开始”
  • 在行中循环,直到列中出现风险 C为空(这一点可能位于表的末尾)
  • 根据风险突出显示行
  • 当所有行都完成时,按列“D”升序排序,因为风险“1”为“高”
  • 我通过记录自己所做的事情(正如其他人在这里所建议的那样),制作了很多这段代码,然后编辑了一些单元格引用,使其“健壮”以供您使用

    我希望这有帮助


    Paul

    初级,我亲爱的Watson,使用你的键盘。谢谢你,我已经通过录制宏找到了一些帮助。我的宏只有一个问题,在OP中共享,你能帮我解决吗?