looping

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

How would I write the macro to count only until it comes across a false
statement in a cell.
IE:
count cells ag51:ag54 until false.
ag51 =true
ag52=true
ag53= false
ag54 = true

the macro would return a 2 for ag51 and ag52 then stop on ag53 because it is
equal to false.

Any help would be appreciated.
Eric
 
maybe something like this, just change the range

Sub count_true()
Dim cell As Range
Dim rng As Range
Dim counter As Long
counter = 0

Set rng = Worksheets("Sheet1").Range("A1:A4")
For Each cell In rng
If cell.Value = True Then
counter = counter + 1
Else
Exit For
End If
Next

MsgBox counter
End Sub
 
here's another way, just change i to your starting row:

Sub count_true()
Dim counter As Long
Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
i = 1
Do While ws.Range("A" & 9) = True
counter = counter + 1
i = i + 1
Loop
MsgBox counter
End Sub
 
This is fantastic but the only thing is I need it in a cell not in a message
box. And the cell would say how ever many tests are off. IE the number that
it's counting.
Eric
 
Assign the count to a cell using.....

Range("A5") = counter



--

Regards,
Nigel
(e-mail address removed)
 
Your OP asked for the count up until the first false, so the counter value
is the count, is it not?


--

Regards,
Nigel
(e-mail address removed)
 
something like this
Sub countuntil()
For i = 15 To 19

If Cells(i, "a") = "True" Then mc = mc + 1
If Cells(i, "a") = "False" Then Exit For
Next i
MsgBox mc
End Sub
 
This function will return the number of TRUE values prior to a FALSE value
in a specified range...

Function CountTrueValues(RangeIn As Range) As Long
CountTrueValues = RangeIn.Find(False).Row - RangeIn.Row
End Function

Here is an example of how to use this in your own code...

Sub Test()
MsgBox "Number of TRUEs = " & CStr(CountTrueValues(Range("AG51:AG54")))
End Sub

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top