Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default change code to work over multiple sheets


I have a function:

Code:
--------------------
Public Function PositionCount(pos As String, rge As Range) As Integer

Dim counter As Integer

counter = 0

For Each r In rge.Cells

If r.Value = pos Then counter = counter + 1

Next r

PositionCount = counter


End Function
--------------------

The function is used like this:
=PositionCount(A4,'1'!L13:L300)
Cell A4 simply contains a string. This string is what is being
'searched' for.

I have 31 sheets, named 1 .. 31

At the moment I have an annoyingly long formula like this:
=PositionCount(A4,'31'!L13:L300)+
...
PositionCount(A4,'1'!L13:L300)
Each of those checks the same range on a different sheet.

How should my code be changed so that it will do exactly the same
thing, but look up the given range on the 31 different sheets?


--
cda_cmd
------------------------------------------------------------------------
cda_cmd's Profile: http://www.excelforum.com/member.php...o&userid=31543
View this thread: http://www.excelforum.com/showthread...hreadid=512401

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default change code to work over multiple sheets

Hi,

what about if you change the code this way:

Code:
--------------------
Public Function PositionCount(pos As String, rge As Range) As Integer

Dim counter As Integer

counter = 0

For Each shtSheet in Activeworkbook.Sheets

For Each r In rge.Cells

If r.Value = pos Then counter = counter + 1

Next r

Next shtSheet

PositionCount = counter

End Function
--------------------

Make sure you change your formula from the value '31'!L13:L300 to L13:L300
for rge.

Did this help you?

Best wishes,
Eric

"cda_cmd" wrote:


I have a function:

Code:
--------------------
Public Function PositionCount(pos As String, rge As Range) As Integer

Dim counter As Integer

counter = 0

For Each r In rge.Cells

If r.Value = pos Then counter = counter + 1

Next r

PositionCount = counter


End Function
--------------------

The function is used like this:
=PositionCount(A4,'1'!L13:L300)
Cell A4 simply contains a string. This string is what is being
'searched' for.

I have 31 sheets, named 1 .. 31

At the moment I have an annoyingly long formula like this:
=PositionCount(A4,'31'!L13:L300)+
...
PositionCount(A4,'1'!L13:L300)
Each of those checks the same range on a different sheet.

How should my code be changed so that it will do exactly the same
thing, but look up the given range on the 31 different sheets?


--
cda_cmd
------------------------------------------------------------------------
cda_cmd's Profile: http://www.excelforum.com/member.php...o&userid=31543
View this thread: http://www.excelforum.com/showthread...hreadid=512401


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default change code to work over multiple sheets

Function CountifAcross(vCriteria, sFirst As String, _
sLast As String, vRng) As Long
Dim ws As Worksheet
Dim sAddr As String
Dim sCtrit As String
Dim nCnt As Long

Application.Volatile ' seems to require this

If TypeOf vRng Is Range Then
sAddr = vRng.Address
Else
sAddr = vRng
End If

scrit = "=" & CStr(vCriteria)
For Each ws In Worksheets
If ws.Name = sFirst Then b = True
If b Then
nCnt = nCnt + Application.CountIf(ws.Range(sAddr), scrit)
End If
If ws.Name = sLast Then Exit For
Next

CountifAcross = nCnt
End Function

Be careful if moving sheets.

If you search "Countif Across *Sheets" over in excel.misc or
..worksheetfunctions there are plenty of formulas that would avoid needing a
UDF (but require populating a list of sheet names)

Regards,
Peter T

"cda_cmd" wrote in
message ...

I have a function:

Code:
--------------------
Public Function PositionCount(pos As String, rge As Range) As Integer

Dim counter As Integer

counter = 0

For Each r In rge.Cells

If r.Value = pos Then counter = counter + 1

Next r

PositionCount = counter


End Function
--------------------

The function is used like this:
=PositionCount(A4,'1'!L13:L300)
Cell A4 simply contains a string. This string is what is being
'searched' for.

I have 31 sheets, named 1 .. 31

At the moment I have an annoyingly long formula like this:
=PositionCount(A4,'31'!L13:L300)+
..
PositionCount(A4,'1'!L13:L300)
Each of those checks the same range on a different sheet.

How should my code be changed so that it will do exactly the same
thing, but look up the given range on the 31 different sheets?


--
cda_cmd
------------------------------------------------------------------------
cda_cmd's Profile:

http://www.excelforum.com/member.php...o&userid=31543
View this thread: http://www.excelforum.com/showthread...hreadid=512401



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
multiple sheets 2 work books capt c Excel Worksheet Functions 0 April 8th 09 03:03 PM
Counting dates in multiple work sheets and work books Savage Excel Discussion (Misc queries) 0 December 19th 05 11:41 PM
HOW TO: One name range to work for multiple sheets Dan Thompson Excel Programming 0 November 16th 05 10:04 PM
tried to change my code but cant get it to work. Rob Excel Programming 2 February 12th 05 07:59 PM
Multiple Work Sheets Daniell Excel Worksheet Functions 4 January 26th 05 02:08 AM


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

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"