Home > Back-end >  Change Row Color in Excel Range if condition is met in vb.Net
Change Row Color in Excel Range if condition is met in vb.Net

Time:10-27

In range "A" to "H" I want to change color when condition in H cell is met. My code work, but it change entire row color if I use this one:

    For Each h In shXL.Range("A2:H150").Columns(8).Cells
                If h.Value > 14 Then
                    h.EntireRow.Interior.Color = Color.FromArgb(251, 254, 138)
                End If
    Next

Or color changed with shift from H to N, when I use this one:

For Each h In shXL.Range("A2:H150").Columns(8).Cells
            If h.Value > 14 Then
                h.Range("A1:H1").Interior.Color = Color.FromArgb(251, 254, 138)
            End If
Next

How to do it?

CodePudding user response:

You could do it various ways, such as the following.

In your example A1:H1 is relative to column H, hence H to N rather than A to H.

For Each h In shXL.Range("H2:H150")
   If h.Value > 14 Then
       h.offset(,-7).resize(,8).Interior.Color = Color.FromArgb(251, 254, 138)
   End If
Next
  • Related