根据值更改颜色

时间:2017-02-20 16:37:44

标签: excel vba colors conditional-formatting selectionchanged

我正在制作一个包含三个按钮的表单和一个小型数据电子表格。每个按钮将与不同范围的可接受结果相对应。当您按下第一个按钮时,我希望它将工作表中的结果突出显示为绿色(如果它们介于38和44.4之间),并将该范围内的结果突出显示为红色。对于第二个按钮,我希望良好的范围是33到39.4,第三个按钮是33到39.4(相同的可接受结果范围,不同类型的测试)。基本上我需要使用每个按钮来制定不同的条件格式。我目前正在使用:

Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Range("C16") > 44.4 Then Target.Interior.Color = vbRed
If Target.Range("C16") < 38 Then Target.Interior.Color = vbRed
If Target.Range("C16") >= 38 And Target <= 44.4 Then Target.Interior.Color = vbGreen


End Sub

我不熟悉选择更改属性,但这无论如何都不起作用。不知道从哪里开始。任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:4)

44.4大于或等于38,无论其他条件的结果如何,对它的检查都在运行,这意味着任何大于或等于38的值都将变为绿色。

您需要更好地指定您的条件,特别是X和Y之间的&#34;&#34;部分。这看起来像Select Case块的作业 - 例如,这将是38和44.4之间的#34;&#34;代码:

Select Case ActiveSheet.Cells("C16").Value
    Case 38 To 44.4
       Target.Interior.Color = vbGreen
    Case Else
       Target.Interior.Color = vbRed
End Select

&#34;绿色在33和39.4之间&#34;代码看起来像这样:

Select Case ActiveSheet.Cells("C16").Value
    Case 33 To 39.4
       Target.Interior.Color = vbGreen
    Case Else
       Target.Interior.Color = vbRed
End Select

注意一个模式?对其进行参数化并将其放入自己的程序中:

Public Sub ConditionalHighlight(ByVal checkCell As Range, ByVal target As Range, ByVal lowerLimit As Double, ByVal upperLimit As Double)
    'ensure the value is a Double (and not an error or a string):
    If VarType(checkCell.Value) <> vbDouble Then Exit Sub

    'work with an actual Double instead of the Variant we get off the cell:
    Dim checkValue As Double
    checkValue = CDbl(checkCell.Value)

    Select Case checkValue
        Case lowerLimit To upperLimit
            target.Interior.Color = vbGreen
        Case Else
            target.Interior.Color = vbRed
    End Select

End Sub

现在只需通过按钮拨打电话即可。 Click处理程序:

Private Sub Button1_Click()
    ConditionalHighlight Range("C16"), Range("whatever"), 38, 44.4
End Sub

Private Sub Button2_Click()
    ConditionalHighlight Range("C16"), Range("whatever"), 33, 39.4
End Sub

Private Sub Button3_Click()
    ConditionalHighlight Range("C16"), Range("whatever"), 42, 74.4
End Sub

答案 1 :(得分:-1)

点击按钮:

 <?php

#API access key from Google API's Console

    define( 'API_ACCESS_KEY', 'API_KEY' );

    $topic = "/topics/todos";

$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
        'to' => $topic,
        'notification' => array (
                "body" => "nova casa adicionada",
                "title" => "Nova casa",
                "sound"=> "default",
                "click_action"=> "casaactivity"
        ),
        'data' => array(
            "localcasa" => "local",
            "precocasa"=> "Preço",
            "imgurl"=> "link imagem",
            "infocs"=> "info",
            "csid"=> "id da casa"
        )
);
$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );

?>