test a range of cells with if statement in vba

dcasebolt

New Member
Joined
Feb 24, 2012
Messages
15
Hello,
I am trying to figure out how to test some cells in excel using VBA code to change a color of a cell based on the results of a test. I have it figured out for one cell but have been unable to figure out how to test multiple cells using the same format. Using Excel 2007 software. Tried using range and keep getting a type mismatch 13 error. Used both Range("A1:B7") and [A1:B7] with no luck. Below is my code that works with one cell being tested:

Sub Cell_Color()
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")

If Cells(2, 2).value = "x" Then Cells(1, 1).Interior.ColorIndex = 3
If Cells(2, 2).value = "d" Then Cells(1, 1).Interior.ColorIndex = 6
If Cells(2, 2).value = " " Then Cells(1, 1).Interior.ColorIndex = 4
End Sub

The goal is to test multiple cells for same values and have each one independently respond with the appropriate color code.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This will work for you.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ranCol()<br><SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("A1:IV600")<br>    <SPAN style="color:#00007F">If</SPAN> cell.Value = "x" <SPAN style="color:#00007F">Then</SPAN><br>    cell.Interior.ColorIndex = 3<br>    <SPAN style="color:#00007F">ElseIf</SPAN> cell.Value = "d" <SPAN style="color:#00007F">Then</SPAN><br>    cell.Interior.ColorIndex = 6<br>    <SPAN style="color:#00007F">ElseIf</SPAN> cell.Value = " " <SPAN style="color:#00007F">Then</SPAN><br>    cell.Interior.ColorIndex = 4<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Try
Code:
Sub Cell_Color()
Dim ws As Worksheet
Dim c As Range

Set ws = ActiveSheet

For Each c In ActiveSheet.Range("B2:J10")
    If c.Value = "x" Then c.Interior.ColorIndex = 3
    If c.Value = "d" Then c.Interior.ColorIndex = 6
    If c.Value = " " Then c.Interior.ColorIndex = 4
Next c

End Sub

Note: this change the color of each cell being tested rather than the single cell A1.
 
Upvote 0
Hi

Remark: you don't need code for this, you can use Conditional Formatting directly in the worksheet.
 
Upvote 0
This will work for you.


Sub ranCol()
Dim cell As Range
For Each cell In Range("A1:IV600")
If cell.Value = "x" Then
cell.Interior.ColorIndex = 3
ElseIf cell.Value = "d" Then
cell.Interior.ColorIndex = 6
ElseIf cell.Value = " " Then
cell.Interior.ColorIndex = 4
End If
Next
End Sub



Hi, I am facing a similar problem.

Starting from the mentioned code, could it be possible to make something like
If cell.Value = value of the cell to its left Then...?

So that, any cell which has the same value as the one on its left, will be highlighted with a color?

Thats my code which does not work:

Sub format()
Dim cell As Range
For Each cell In Range("B3:AU110")
If cell(2, 3).Value = cell(2, 2).Value Then
cell.Interior.ColorIndex = 10
Else
cell.Interior.ColorIndex = 0
End If
Next
End Sub


Many thanks in advance!
 
Last edited:
Upvote 0
As pgc01 pointed out in the old thread, you don't need vba to achieve this result. Is there some particular reason you couldn't use Excel's standard Conditional Formatting?
 
Upvote 0
Following context:

I am building a data base in Excel which should show official valuations of a security at a certain quarter in time. However, there might be a quarter where no official valuation is available. In such a situation, the valuation should be equal to the previous quarter and it should be highlighted.

If I use duplicate values (conditional formatting)for example, it would work out. However it would highlight all cells having the same value even if they are not be equal to its previous quarter at that point in time.

As a consequence, it should only refer to its left cell (previous quarter) and it should auto-adjust cell per cell:
Look up if C3=B3, D3=C3... and so on for a few hundred cell values.

I did not find any possibility to make it as precise using conditional formatting. If you have an idea, please enlighten me :)
 
Upvote 0
I did not find any possibility to make it as precise using conditional formatting. If you have an idea, please enlighten me :)
See if this pretty simple CF formula does what you want.
Column A can never be highlighted, so select from B2 down to J11 and apply the CF formula shown. Use New Rule -> Use a formula to determine ...

Excel Workbook
ABCDEFGHIJ
1
27563371787
31488498194
45716536633
58869999935
61971611831
73439943226
84473622615
99384396641
106798159475
115544313914
CF Left
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B21. / Formula is =B2=A2Abc
 
Upvote 0
Unfortunately I do not get along about how to enter the formula.
My conditional formatting box seems to be different than yours? I am using EXCEL 2013

There is only field mentioning:
"Format values where this formula is true"


How should it be entered in my layout?

Thank you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top