How to color code a cell that is labeled as a variant in VBA coding

时间:2019-01-18 18:48:18

标签: vba colors variant

I have a form that the user fills out and and need to create a color coded grid based on their answer. I can get the cell identified that needs to be colored but cannot get the color to appear properly.

Dim PlantName As String
Dim DateEntered As String
Dim PlantRow As Integer
Dim DateColumn As Integer
Dim Address As Variant


PlantName = Me.ComboBox1.Value
DateEntered = Me.TextBox5.Value

PlantRow = Sheets("Expedite").Range("A3:A5").Find(PlantName).row
DateColumn = Sheets("Expedite").Range("B2:BB2").Find(DateEntered).Column

Address = Cells(PlantRow, DateColumn).Address

If Me.Expedite_yes.Value = True Then
    Address.Interior.Color = RGB(0, 255, 0) And Address.Text.Color = RGB(0, 225, 0)
    ElseIf Me.Expedite_yes.Value = False Then
    Address.Interior.Color = RGB(255, 0, 0) And adress.Text.Color = RGB(255, 0, 0)
End If

The cell found in "address should be colored green in both interior and text if me.expedite_yes.value = true but I am getting a syntax error on the address.interior.color line.

2 个答案:

答案 0 :(得分:2)

Main issue: You need to modify the Interior.Color and .Font.Color of a Range object, not a String address. Also as noted in the comments, remove And and put the two items as separate lines.

If you were to keep your initial approach, that might look like:

Dim rng as Range
...
Set rng = Cells(PlantRow, DateColumn)
...
rng.Interior.Color = RGB(0, 255, 0)
rng.Font.Color = RGB(0, 225, 0)

Other issues:

  1. Both PlantRow = Sheets("Expedite").Range("A3:A5").Find... and DateColumn = Sheets("Expedite").Range("B2:BB2").Find assume that the PlantName and DateEntered are actually found, and will throw an error if they are not found.
  2. You should qualify which Worksheet - assuming it's Sheets("Expedite") that the Cells are on. Otherwise there is an implicit reference to the ActiveSheet.

With those modifications, your code might look like this:

Dim PlantName As String
Dim DateEntered As String

PlantName = Me.ComboBox1.Value
DateEntered = Me.TextBox5.Value

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Expedite")

Dim plantRng As Range, dateRng As Range
Set plantRng = ws.Range("A3:A5").Find(PlantName)
Set dateRng = ws.Range("B2:BB2").Find(DateEntered)

If Not plantRng Is Nothing Then
    If Not dateRng Is Nothing Then
        Dim rng As Range
        Set rng = ws.Cells(plantRng.Row, dateRng.Column)
    Else
        MsgBox "Not found - try again!"
        Exit Sub
    End If
End If

If Me.Expedite_yes.Value = True Then
    rng.Interior.Color = RGB(0, 255, 0)
    rng.Font.Color = RGB(0, 225, 0)
ElseIf Me.Expedite_yes.Value = False Then
    rng.Interior.Color = RGB(255, 0, 0)
    rng.Font.Color = RGB(255, 0, 0)
End If

答案 1 :(得分:0)

The problem is that when you define your address, variable "Address" receives the string of the address like "B2". You need to create a range object from this address string like in the code below. For the Font color I get .Font.Color working like this:

Option Explicit

Sub test()
Dim addr As Variant
Dim TestBool As Boolean

addr = Cells(2, 2).Address
Debug.Print addr
TestBool = True
If TestBool = True Then
    Range(addr).Interior.Color = RGB(0, 255, 0) And Range(addr).Font.Color = RGB(0, 255, 0)
End If

End Sub