PC Review


Reply
Thread Tools Rate Thread

Creating a Macros function but co9ntent of function only works in a sub plz help

 
 
Alexandre Brisebois (www.pointnetsolutions.com)
Guest
Posts: n/a
 
      20th Jul 2005
Hey, I am creating a function that need to go read off of different
sheets andsum up a specific cell on all active sheets defined on the
first summary sheet.

the problem is that the code works great in a sub since i have to move
from sheet to sheet but when i get to a function it simply does not
work any more...

to find the cell i need to sum i have to locate specific content of a
cell in a cpecific column in this case the column is A & i must look
for the content of the cells then offset from it and sum the value on
all th esheets specified on the frist sheet where the names of the
sheets are available.


Any ideas?


Code --->>>

Public Function SumTotal(ByRef cellref As Object, offsetBy As Integer)
On Error Resume Next

Dim accumulator As Double

Dim start As String
start = ActiveSheet.Name

Dim InRange As Range

'get the active sheet names.
Set InRange = Intersect(ActiveSheet.UsedRange, Columns("AX:AX"))

Dim Rng As Range
Dim tempRng As Range

'Application.Volatile True

'sum all sheet values
For Each Rng In InRange.Cells
If Not (IsEmpty(Rng)) Then
Sheets(Rng.Value).Select
Range("A1").Select
Cells.Find(What:=cellref.Value, After:=ActiveCell,
LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(rowOffset:=0,
columnOffset:=offsetBy).Activate
accumulator = accumulator + ActiveCell.Value
End If
Next Rng

Sheets(start).Select
SumTotal = accumulator
End Function

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      20th Jul 2005
First, what version of excel are you using?

xl2002 was the first version to allow you to use .find in a UDF called from a
worksheet.

And you could get rid of all the .selects and activates. And add that
"application.volatile" line back in. You're not passing enough to the function
to keep track if things change in those other worksheets.

So that means that this function could be one calculation behind being accurate.

This did work ok in xl2003:

Option Explicit

Public Function SumTotal(ByRef cellref As Range, offsetBy As Long)

Application.Volatile

Dim accumulator As Double
Dim testWks As Worksheet
Dim Rng As Range
Dim tempRng As Range
Dim InRange As Range
Dim FoundCell As Range

'get the active sheet names.
With Application.Caller.Parent
Set InRange = Intersect(.UsedRange, .Columns("AX:AX"))
End With

'sum all sheet values
For Each Rng In InRange.Cells
If Not (IsEmpty(Rng)) Then
Set testWks = Nothing
On Error Resume Next
Set testWks = Worksheets(Rng.Value)
On Error GoTo 0
If testWks Is Nothing Then
'not a good worksheet name
Else
With testWks
Set FoundCell = .Cells.Find(What:=cellref.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If FoundCell Is Nothing Then
'do nothing
Else
With FoundCell.Offset(0, offsetBy)
If IsNumeric(.Value) Then
accumulator = accumulator + .Value
End If
End With
End If
End With
End If
End If
Next Rng
SumTotal = accumulator
End Function

"Alexandre Brisebois (www.pointnetsolutions.com)" wrote:
>
> Hey, I am creating a function that need to go read off of different
> sheets andsum up a specific cell on all active sheets defined on the
> first summary sheet.
>
> the problem is that the code works great in a sub since i have to move
> from sheet to sheet but when i get to a function it simply does not
> work any more...
>
> to find the cell i need to sum i have to locate specific content of a
> cell in a cpecific column in this case the column is A & i must look
> for the content of the cells then offset from it and sum the value on
> all th esheets specified on the frist sheet where the names of the
> sheets are available.
>
> Any ideas?
>
> Code --->>>
>
> Public Function SumTotal(ByRef cellref As Object, offsetBy As Integer)
> On Error Resume Next
>
> Dim accumulator As Double
>
> Dim start As String
> start = ActiveSheet.Name
>
> Dim InRange As Range
>
> 'get the active sheet names.
> Set InRange = Intersect(ActiveSheet.UsedRange, Columns("AX:AX"))
>
> Dim Rng As Range
> Dim tempRng As Range
>
> 'Application.Volatile True
>
> 'sum all sheet values
> For Each Rng In InRange.Cells
> If Not (IsEmpty(Rng)) Then
> Sheets(Rng.Value).Select
> Range("A1").Select
> Cells.Find(What:=cellref.Value, After:=ActiveCell,
> LookIn:=xlValues, _
> LookAt:=xlWhole, SearchOrder:=xlByRows,
> SearchDirection:=xlNext, _
> MatchCase:=False).Activate
> ActiveCell.Offset(rowOffset:=0,
> columnOffset:=offsetBy).Activate
> accumulator = accumulator + ActiveCell.Value
> End If
> Next Rng
>
> Sheets(start).Select
> SumTotal = accumulator
> End Function


--

Dave Peterson
 
Reply With Quote
 
Alexandre Brisebois (www.pointnetsolutions.com)
Guest
Posts: n/a
 
      20th Jul 2005
Hey Dave,
I am working on excel 2000,

I am limited to its capabilities and simply do not know how i should go
about doing this,

like you can see i grab the names of the sheets. then i cycle through
the names go to the sheets and find the cell containing the value of
the cell i am passing by reference. then i need to offset, get the
value accumulate the value

and move to the next sheet and start ofver again...

i might have something in the wrong order or not using the right
technique.

Regards,
Alexandre

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Jul 2005
If the value that you're looking for is always in the same column of all those
worksheets, you could use application.match to look for it.

If the column varies between worksheets, then you could cycle through all the
columns of each worksheet and look for it (using application.match in a loop).



"Alexandre Brisebois (www.pointnetsolutions.com)" wrote:
>
> Hey Dave,
> I am working on excel 2000,
>
> I am limited to its capabilities and simply do not know how i should go
> about doing this,
>
> like you can see i grab the names of the sheets. then i cycle through
> the names go to the sheets and find the cell containing the value of
> the cell i am passing by reference. then i need to offset, get the
> value accumulate the value
>
> and move to the next sheet and start ofver again...
>
> i might have something in the wrong order or not using the right
> technique.
>
> Regards,
> Alexandre


--

Dave Peterson
 
Reply With Quote
 
Alexandre Brisebois (www.pointnetsolutions.com)
Guest
Posts: n/a
 
      20th Jul 2005
do you have an example of the appluication match ?

i decided to do this using a sub afterall but i am having some
problems..

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Jul 2005
Is it just one column or multiple columns?

I'm gonna guess just one column:

Option Explicit
Public Function SumTotal(ByRef cellref As Range, offsetBy As Long)

Application.Volatile

Dim accumulator As Double
Dim testWks As Worksheet
Dim Rng As Range
Dim tempRng As Range
Dim InRange As Range
Dim FoundCell As Range
Dim res As Variant
Dim ColToLookThrough As Range

'get the active sheet names.
With Application.Caller.Parent
Set InRange = Intersect(.UsedRange, .Columns("AX:AX"))
End With

'sum all sheet values
For Each Rng In InRange.Cells
If Not (IsEmpty(Rng)) Then
Set testWks = Nothing
On Error Resume Next
Set testWks = Worksheets(Rng.Value)
On Error GoTo 0
If testWks Is Nothing Then
'not a good worksheet name
Else
With testWks
Set ColToLookThrough = .Range("a:a")
res = Application.Match(cellref.Value, ColToLookThrough, 0)
If IsError(res) Then
'not found
Else
Set FoundCell = ColToLookThrough(res)
With FoundCell.Offset(0, offsetBy)
If IsNumeric(.Value) Then
accumulator = accumulator + .Value
End If
End With
End If
End With
End If
End If
Next Rng
SumTotal = accumulator
End Function




"Alexandre Brisebois (www.pointnetsolutions.com)" wrote:
>
> do you have an example of the appluication match ?
>
> i decided to do this using a sub afterall but i am having some
> problems..


--

Dave Peterson
 
Reply With Quote
 
Alexandre Brisebois (www.pointnetsolutions.com)
Guest
Posts: n/a
 
      20th Jul 2005
Thabks Dave Im looking through it as we speak,
your help is greatly apreciated.

Regards,
Alexandre

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
The AND Function in Macros tallkris Microsoft Excel Programming 1 19th Jan 2006 06:24 PM
function key macros under XP Larry__Weiss Windows XP General 1 30th May 2005 07:38 AM
F4 function key with macros amelie Microsoft Excel Programming 1 9th Sep 2004 12:29 PM
Dedupe Function works/works not Tom Bock Microsoft Excel Worksheet Functions 5 17th Mar 2004 08:41 PM
Javascript function works but C# function doesn't execute Jim Hammond Microsoft ASP .NET 2 14th Nov 2003 08:43 PM


Features
 

Advertising
 

Newsgroups
 


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