我唯一需要修改的是下面列出的公式。我修改了代码,将条件格式应用于我的范围内属于两个值(输入值1和2)之间的任何单元格值。例如,如果我要搜索20,000到50,000之间的任何单元格值,它会将这些单元格的字体颜色更改为红色和粗体。编码的下一部分使用公式然后在列G中的单元格上应用条件格式。
我可以以某种方式修改下面的公式,以便找到任何大于或等于输入值1但在指定范围内小于或等于输入值2的值吗?该公式目前仅搜索值1的确切值。
=ISNUMBER(SEARCH(" & Chr(34) & Value1 & Chr(34) & ",""&AB1&"",""&AG1&"",""&AL1&"",""&AQ1))"
下面我剪切并粘贴了我的代码
Sub Between_Values()
Dim Value1 As String
Dim Value2 As String
Dim myRange As Range
Dim myFormula As String
Value1 = InputBox("Between This amount (enter lowest contract amount)")
Value2 = InputBox("And This amount (enter highest contract amount)")
Set myRange = Range("G:G,AB:AB,AG:AG,AL:AL,AQ:AQ")
myRange.Cells.FormatConditions.Delete
myRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=" & Value1, Formula2:="=" & Value2
myRange.FormatConditions(myRange.FormatConditions.Count).SetFirstPriority
With myRange.FormatConditions(1).Font
.Italic = False
.Bold = True
.Color = 255
.TintAndShade = 0
End With
myFormula = "=ISNUMBER(SEARCH(" & Chr(34) & Value1 & Chr(34) & ",""&AB1&"",""&AG1&"",""&AL1&"",""&AQ1))"
Columns("G:G").FormatConditions.Add Type:=xlExpression, Formula1:=myFormula
Columns("G:G").FormatConditions(Columns("G:G").FormatConditions.Count).SetFirstPriority
With Columns("G:G").FormatConditions(1).Font
.Bold = True
.Color = 255
.TintAndShade = 0
End With
Columns("G:G").FormatConditions(1).StopIfTrue = False
End Sub
答案 0 :(得分:0)
一般的想法是循环遍历范围并在符合这些标准的任何时候格式化当前单元格
Dim cel As Range
For Each cel In myRange .Cells
If cel.value >= Value1 And cel.value <= Value2 Then
With cel
'do formatting
End With
End If
Next cel