ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   looping (https://www.excelbanter.com/excel-programming/402072-looping.html)

Eric

looping
 
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

Gary Keramidas[_2_]

looping
 
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

--


Gary Keramidas


"Eric" wrote in message
...
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



Gary Keramidas[_2_]

looping
 
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


--


Gary Keramidas


"Eric" wrote in message
...
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



Eric

looping
 
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

"Gary Keramidas" wrote:

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

--


Gary Keramidas


"Eric" wrote in message
...
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




Nigel[_2_]

looping
 
Assign the count to a cell using.....

Range("A5") = counter



--

Regards,
Nigel




"Eric" wrote in message
...
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

"Gary Keramidas" wrote:

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

--


Gary Keramidas


"Eric" wrote in message
...
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





Nigel[_2_]

looping
 
Your OP asked for the count up until the first false, so the counter value
is the count, is it not?


--

Regards,
Nigel




"Eric" wrote in message
...
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

"Gary Keramidas" wrote:

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

--


Gary Keramidas


"Eric" wrote in message
...
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





Don Guillett

looping
 
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

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Eric" wrote in message
...
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



Rick Rothstein \(MVP - VB\)

looping
 
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



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




All times are GMT +1. The time now is 11:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com