Excel VBA - 单元格颜色更改

时间:2017-05-25 20:36:15

标签: excel-vba vba excel

我正在尝试编写一个基于列L中的值更改颜色的宏。如果列L中的单元格为YES,则Hightlight B列单元格为红色。但是,下面的宏不起作用或失败。它运行但什么也没做。

   Sub ColorMeElmo()
   Dim i As Long, r1 As Range, r2 As Range
   For i = 2 To 5
   Set r1 = Range("L" & i)
   Set r2 = Range("B" & i & ":B" & i)
   If r1.Value = "YES" Then r2.Interior.Color = vbRed
   Next i
   End Sub 

2 个答案:

答案 0 :(得分:1)

将此放入您想要观看的工作表中。

val schema1 = StructType(Array(
  StructField("AcutionId", StringType, true),
  StructField("Bid", IntegerType, false),
  StructField("BidTime", FloatType, false),
  StructField("Bidder", StringType, true),
  StructField("BidderRate", FloatType, false),
  StructField("OpenBid", FloatType, false),
  StructField("Price", FloatType, false),
  StructField("Item", StringType, true),
  StructField("DaystoLive", IntegerType, false)
))

答案 1 :(得分:1)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CCell As Range
    Dim sht As Worksheet
    Set CCell = Range("L:L")
    Set sht = Worksheets("SheetName") 
    If Target.Count = 1 Then
        If Not Application.Intersect(CCell, Range(Target.Address)) _
            Is Nothing Then
                If Target.Value = "YES" Then
                    sht.Cells(Target.Row, 2).Interior.Color = RGB(255, 0, 0)
                End If
        End If
    End If
End Sub