Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Increment a variable while looping

I have a procedure that loops through a range...I want this variable to
increment each time it comes upon an instance of something.
Ex:
For each cell in range
if cell.hasformula then
n="something"
end if
next cell

I want n to hold the value until it is finished looping and have a count of
the number of times cell.hasformula was true



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Increment a variable while looping

You could loop through the formulas:

Option Explicit
Sub testme01()

Dim FormulaCtr As Long
Dim myCell As Range
FormulaCtr = 0
For Each myCell In Selection.Cells
If myCell.HasFormula Then
FormulaCtr = FormulaCtr + 1
End If
Next myCell

End Sub

Or just look at the cells that contain formulas:

Sub testme02()

Dim myRng As Range
Dim FormulaCtr As Long

On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeFormulas))
On Error GoTo 0

If myRng Is Nothing Then
FormulaCtr = 0
Else
FormulaCtr = myRng.Cells.Count
End If

End Sub

ibeetb wrote:

I have a procedure that loops through a range...I want this variable to
increment each time it comes upon an instance of something.
Ex:
For each cell in range
if cell.hasformula then
n="something"
end if
next cell

I want n to hold the value until it is finished looping and have a count of
the number of times cell.hasformula was true


--

Dave Peterson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Increment a variable while looping

Or even:

Sub testme02()
Dim FormulaCtr As Long
On Error Resume Next
FormulaCtr = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeFormulas)). count
On Error GoTo 0
End Sub


--
Regards,
Tom Ogilvy


"Dave Peterson" wrote in message
...
You could loop through the formulas:

Option Explicit
Sub testme01()

Dim FormulaCtr As Long
Dim myCell As Range
FormulaCtr = 0
For Each myCell In Selection.Cells
If myCell.HasFormula Then
FormulaCtr = FormulaCtr + 1
End If
Next myCell

End Sub

Or just look at the cells that contain formulas:

Sub testme02()

Dim myRng As Range
Dim FormulaCtr As Long

On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeFormulas))
On Error GoTo 0

If myRng Is Nothing Then
FormulaCtr = 0
Else
FormulaCtr = myRng.Cells.Count
End If

End Sub

ibeetb wrote:

I have a procedure that loops through a range...I want this variable to
increment each time it comes upon an instance of something.
Ex:
For each cell in range
if cell.hasformula then
n="something"
end if
next cell

I want n to hold the value until it is finished looping and have a count

of
the number of times cell.hasformula was true


--

Dave Peterson



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 Maggie[_6_] Excel Discussion (Misc queries) 6 October 2nd 08 09:14 PM
Not Looping Roger Excel Discussion (Misc queries) 0 February 26th 08 05:18 PM
Looping David T Excel Discussion (Misc queries) 2 August 30th 06 10:51 PM
variable height variable width stacked bar charts ambthiru Charts and Charting in Excel 3 January 18th 06 11:41 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM


All times are GMT +1. The time now is 09:50 AM.

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

About Us

"It's about Microsoft Excel"