Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 满足条件时不改变颜色的If语句_Excel_Vba_Colors - Fatal编程技术网

Excel 满足条件时不改变颜色的If语句

Excel 满足条件时不改变颜色的If语句,excel,vba,colors,Excel,Vba,Colors,我正在使用Excel VBA尝试解决以下问题: 在A栏中,我看到了42个国家的名单。在D栏中,我得到了该国巨无霸的美元价格。第1行有标题,因此数据从第2行开始。我需要建立一个宏,允许用户输入2个国家(Country1和Country2),将通过a列循环查找用户输入的国家及其相应的价格。它应该将国家的单元格位置保存到某个变量中,而价格只是一个数字。如果Country1的价格高于Country2,则Country1的名称的字体颜色应为绿色,Country2的字体颜色应为红色。反之亦然 现在,整个代码

我正在使用Excel VBA尝试解决以下问题:

在A栏中,我看到了42个国家的名单。在D栏中,我得到了该国巨无霸的美元价格。第1行有标题,因此数据从第2行开始。我需要建立一个宏,允许用户输入2个国家(Country1和Country2),将通过a列循环查找用户输入的国家及其相应的价格。它应该将国家的单元格位置保存到某个变量中,而价格只是一个数字。如果Country1的价格高于Country2,则Country1的名称的字体颜色应为绿色,Country2的字体颜色应为红色。反之亦然

现在,整个代码正在运行。但是细胞的颜色没有改变

如果您想测试它,下面是工作表的顶部:


问题是您没有在
Do
循环中设置
单元格,因此它不会从
单元格(2,1)
更改。将其移入:

Counter = 2

Do
    Set TheCell = Cells(Counter, 1)
    TheCell.Select

    If ActiveCell.Value = Country1 Then
更好的是,完全抛弃循环并利用
。查找

Option Explicit
Sub CountryComparison()

    Dim Country1 As String
    Dim Country2 As String
    Dim Price1Cell As Range
    Dim Price2Cell As Range
    Dim Price1 As Single
    Dim Price2 As Single

    Range("A:A").Font.Color = vbBlack

    Country1 = InputBox("Enter Country 1")
    Country2 = InputBox("Enter Country 2")

    Set Price1Cell = Range("A" & Columns("A:A").Find(What:=Country1).Row)
    Set Price2Cell = Range("A" & Columns("A:A").Find(What:=Country2).Row)

    Price1 = Range("A" & Columns("A:A").Find(What:=Country1).Row).Offset(0, 3).Value
    Price2 = Range("A" & Columns("A:A").Find(What:=Country2).Row).Offset(0, 3).Value

    If Price1 > Price2 Then
        Price1Cell.Font.Color = vbRed
        Price2Cell.Font.Color = vbGreen
    Else
        Price1Cell.Font.Color = vbGreen
        Price2Cell.Font.Color = vbRed
    End If

End Sub
Option Explicit
Sub CountryComparison()

    Dim Country1 As String
    Dim Country2 As String
    Dim Price1Cell As Range
    Dim Price2Cell As Range
    Dim Price1 As Single
    Dim Price2 As Single

    Range("A:A").Font.Color = vbBlack

    Country1 = InputBox("Enter Country 1")
    Country2 = InputBox("Enter Country 2")

    Set Price1Cell = Range("A" & Columns("A:A").Find(What:=Country1).Row)
    Set Price2Cell = Range("A" & Columns("A:A").Find(What:=Country2).Row)

    Price1 = Range("A" & Columns("A:A").Find(What:=Country1).Row).Offset(0, 3).Value
    Price2 = Range("A" & Columns("A:A").Find(What:=Country2).Row).Offset(0, 3).Value

    If Price1 > Price2 Then
        Price1Cell.Font.Color = vbRed
        Price2Cell.Font.Color = vbGreen
    Else
        Price1Cell.Font.Color = vbGreen
        Price2Cell.Font.Color = vbRed
    End If

End Sub