根据当前值设置单元格颜色

时间:2020-02-14 08:34:37

标签: excel vba

如何简化我的代码?

如果用户用黄色填充该单元格,则如果其值为0,则它​​将变为红色并弹出一个消息框,然后,如果其值> 0,它将再次返回为黄色,如果用户在“无填充单元格”中输入> 0的值时,如果我输入0,则此代码将变为灰色,然后变为无填充。此代码仅适用于L列,我也需要针对M,N和O列进行设置。

 $url = 'https://something.firebaseio.com/iochatting/roomchats/data.json?auth=private_key';
 $json = json_encode($msg);
 $headers = array();
 $headers[] = 'Content-Type: application/json';
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 curl_setopt($ch, CURLOPT_TIMEOUT, 20500);
 $response = curl_exec($ch);
 if ($response === FALSE) {
 }
 curl_close($ch);

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码。据我了解,您的意图应该按照您的意愿进行。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Tmp As Long

    With Target
        If .Cells.CountLarge > 1 Then Exit Sub
        If (.Column >= Columns("L").Column) And (.Column <= .Columns("O").Column) Then
            Tmp = Val(.Value)
            Select Case .Row
                Case 15
                    .Interior.ColorIndex = IIf(Tmp, 6, 3)
                    If Tmp = 0 Then
                        MsgBox "Project Delay!", _
                               vbCritical, "Attention required!"
                    End If
                Case 17
                    .Interior.ColorIndex = IIf(Tmp, 16, -4142)
                    If Tmp Then
                        MsgBox "Enter a value of zero.", _
                               vbExclamation, "Overlap!"
                    End If
            End Select
        End If
    End With
End Sub

我保持语法简单,以便您应该能够在需要调整的地方对其进行调整。祝你好运!