#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,670
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 364
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,670
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 364
Default 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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default 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


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping Bob Phillips Excel Programming 0 January 11th 07 04:12 PM
Looping David T Excel Discussion (Misc queries) 2 August 30th 06 10:51 PM
Looping Embalmer Excel Programming 1 January 8th 06 11:51 PM
Help with looping dok112[_55_] Excel Programming 0 November 1st 05 05:09 PM
looping every third row Jason Hancock Excel Programming 5 July 1st 04 08:00 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"