.. I generated a bit of test data (with about 1/3 blank cells out of about one million) with a test data code
Code:
With Range("A5:A1000000")
.Cells = "=if(rand()>.33,int(rand()*50),"""")"
.Value = .Value
End With
and ran the code you provided and the code I provided three times each.
Peter_SSs code timed: 9.03secs, 9.04secs, 9.11secs
mirabeau code timed: 5.74secs, 5.75secs, 5.73secs
Am I wrong somewhere? but would you like to expand on your "... takes only about half as long", since this doesn't seem to be correct?
mirabeau
My original test data was just a mixture of blanks, text and integers like below, manually entered, then copied down to row 1,000,000
Excel Workbook |
---|
|
---|
| A |
---|
5 | |
---|
6 | gd |
---|
7 | dfg |
---|
8 | |
---|
9 | g |
---|
10 | fg |
---|
11 | |
---|
12 | |
---|
13 | dfgs |
---|
14 | 2 |
---|
15 | 6 |
---|
16 | 0 |
---|
|
---|
I've just retested that and numerical values generated by your random number formula 4 times using each code. My results were, in seconds:
Peter_SSs code: 3.805, 3.758, 3.789, 3.699
mirabeau code: 7.254, 7.160, 7.051, 7.207
I don't have an explanation for the turn-around of the results between your tests and mine.
Do you have any ideas?
One thing I didn't mention is that I tested on Excel 2010 whereas the OP is using 2007. What were you using?
I have now been able to test on Excel 2007 and in a test on each set of data with each code I got:
Peter_SSs: 5.477, 6.254
mirabeau: 6.348, 6.645
Interestingly, though mine still tested slightly faster than yours, mine was considerably slower on 2007 v 2010 while yours was faster.
CaliKidd
Would be interested in your speed results.
However, given your confirmation that data is only in column A, this is a marginally faster (for me anyway
) version of my earlier code.
<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ColourAndCountBlanks_2()<br> <SPAN style="color:#00007F">Dim</SPAN> rws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, z <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br> <SPAN style="color:#00007F">Const</SPAN> fr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 5<br> <SPAN style="color:#00007F">Const</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 1000000<br> <br> rws = lr - fr + 1<br> Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br> <SPAN style="color:#00007F">With</SPAN> Cells(fr, "A").Resize(rws, 2)<br> .Cells(1, 2).Value = 1<br> .Offset(, 1).Resize(, 1).DataSeries<br> .Sort Key1:=.Cells(1, 1), _<br> Order1:=xlAscending, Header:=xlNo<br> z = .Cells(1, 1).End(xlDown).Row<br> c = lr - z<br> <SPAN style="color:#00007F">If</SPAN> c > 0 <SPAN style="color:#00007F">Then</SPAN><br> Cells(z + 1, 1).Resize(c).Interior.ColorIndex = 3<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> .Sort Key1:=.Cells(1, 2), _<br> Order1:=xlAscending, Header:=xlNo<br> .Offset(, 1).Resize(, 1).ClearContents<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br> Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br> MsgBox "Count = " & c<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
In relation to your other problems, this is slower than the 'blanks' and there may be faster ways, but try this for
1. Find cells with a specific value (e.g., 0 (zero))
<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ColourAndCount()<br> <SPAN style="color:#00007F">Dim</SPAN> rws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, c <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, z <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> rA <SPAN style="color:#00007F">As</SPAN> Range, rB <SPAN style="color:#00007F">As</SPAN> Range, rC <SPAN style="color:#00007F">As</SPAN> Range, rBC <SPAN style="color:#00007F">As</SPAN> Range, rABC <SPAN style="color:#00007F">As</SPAN> Range<br> <SPAN style="color:#00007F">Dim</SPAN> b<br><br> <SPAN style="color:#00007F">Const</SPAN> fr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 5<br> <SPAN style="color:#00007F">Const</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 1000000<br> <br> rws = lr - fr + 1<br> <SPAN style="color:#00007F">Set</SPAN> rA = Cells(fr, "A").Resize(rws)<br> <SPAN style="color:#00007F">With</SPAN> rA<br> <SPAN style="color:#00007F">Set</SPAN> rB = .Offset(, 1)<br> <SPAN style="color:#00007F">Set</SPAN> rC = .Offset(, 2)<br> <SPAN style="color:#00007F">Set</SPAN> rABC = .Resize(, 3)<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> rBC = rB.Resize(, 2)<br> b = rA.Value<br> <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> rws<br> <br> <SPAN style="color:#007F00">'*******************************</SPAN><br> <SPAN style="color:#007F00">'Cells = 0</SPAN><br> <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> b(i, 1)<br> <SPAN style="color:#00007F">Case</SPAN> vbNullString<br> b(i, 1) = "#"<br> <SPAN style="color:#00007F">Case</SPAN> 0<br> b(i, 1) = vbNullString<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br> <SPAN style="color:#007F00">'*******************************</SPAN><br> <br> <SPAN style="color:#00007F">Next</SPAN> i<br> Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br> rB.Value = b<br> rC.Cells(1).Value = 1<br> rC.DataSeries<br> rABC.Sort Key1:=rB.Cells(1), Order1:=xlAscending, Header:=xlNo<br> z = rB.Cells(1).End(xlDown).Row<br> c = lr - z<br> <SPAN style="color:#00007F">If</SPAN> c > 0 <SPAN style="color:#00007F">Then</SPAN><br> Cells(z + 1, rA.Column).Resize(c).Interior.ColorIndex = 3<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> rABC.Sort Key1:=rC.Cells(1), Order1:=xlAscending, Header:=xlNo<br> rBC.ClearContents<br> Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br> MsgBox "Count = " & c<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
Then for the other problems, replace the section between the '****** rows with the relevant section from below. If they don't do what you want, post back with more details.
<font face=Courier New><SPAN style="color:#007F00">'Cells with value > 1</SPAN><br><SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> Val(b(i, 1))<br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 1<br> b(i, 1) = vbNullString<br> <SPAN style="color:#00007F">Case</SPAN> 0<br> b(i, 1) = "#"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br><br><SPAN style="color:#007F00">'Cells not equal to A or B or C</SPAN><br><SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> b(i, 1)<br> <SPAN style="color:#00007F">Case</SPAN> "A", "B", "C"<br><br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN><br> b(i, 1) = vbNullString<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br><br><SPAN style="color:#007F00">'Numeric cells</SPAN><br><SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> b(i, 1) <> vbNullString And IsNumeric(b(i, 1))<br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">True</SPAN><br> b(i, 1) = vbNullString<br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">False</SPAN><br> b(i, 1) = "#"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br> <br><SPAN style="color:#007F00">'Text cells</SPAN><br><SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> IsNumeric(b(i, 1))<br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">True</SPAN><br> b(i, 1) = "#"<br> <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">False</SPAN><br> b(i, 1) = vbNullString<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN></FONT>