Sub routine to test and color cells

Closed
Ian - 26 Aug 2009 à 09:19
venkat1926 Posts 1863 Registration date Sunday 14 June 2009 Status Contributor Last seen 7 August 2021 - 27 Aug 2009 à 20:17
Hello,
I am trying to develop a simple function or sub to scan a worksheet of approx 2000 cells, test the contents, if the cell is empty leave, if the cell contains the value 1 leave, and color everything else (e.g incorrect answers) red.

Its been a while since I programmed in BASIC

Many Tahnks

Ian
Related:

2 responses

venkat1926 Posts 1863 Registration date Sunday 14 June 2009 Status Contributor Last seen 7 August 2021 811
26 Aug 2009 à 20:13
your data is from A1 down in column A

try this macro ( modify to suit you)

Sub test()
Dim rng As Range, c As Range
Set rng = Range(Range("A1"), Cells(Rows.Count, "A").End(xlUp))
For Each c In rng
If c <> "" And c <> 1 Then c.Interior.ColorIndex = 3
Next c
End Sub
Thanks for that venkat

However I could not get it to go past the "If c <> line..."

This is a typical column

a
c
c
1
d
a
0
b

I only want to leave 1 and 0. All other answers )a,b,c,d) to be coloured red

Would this command be useful to determine the spreadsheet size i.e. avoid counting column by column?

Worksheets(1).Range("a1:k50")

Hope you can help, again!

Thanks

Ian
venkat1926 Posts 1863 Registration date Sunday 14 June 2009 Status Contributor Last seen 7 August 2021 811
27 Aug 2009 à 20:17
I do not understand this did not work. What do you mean that it stops at C<> line.
it works i n my computer
one moe thing in your first message said that "if the cell is empty leave". but the cell is not empty but has zero(0) in the cell.

so change
c<>""
into
c<>0
check again