Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using dcount function within user-defined worksheet function

Hi All,

I'm writing a function that counts the number of rows that match a composite
criteria. A set of criteria is passed in the function's arguments. See
example:

Function CountCustomers(group as string, status as string) as Integer
dim cr as Range ' Criteria Range
dim Result as Integer

Result = 0

set r = ThisWorkbook.Sheets("Criteria").Range("A1:B2")
r.Range("A1") = group ' Function exits at this line when used in
worksheet
r.Range("B1") = status

For Each s In ThisWorkbook.Sheets
If InStr(s.Name, "CUST")=1 Then
result = result +
WorkSheetFunction.DCountA(s.Range("M8:M11"), "Name", criteriaRange)
End If
Next
CountCustomers = result
End Function

I created a hidden sheet "Criteria" to be used as a selection criteria for
the DCountA function (last argument).

But when I use "=CountCustomer" in the Excel sheet cell, it appeared as
'#Value'. I know that this happens when we try to modify cells in a function.

Is there any way to make the calculation go through without premature
exiting at the remarked point?

Thanks a lot in advance
pongthai
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Using dcount function within user-defined worksheet function

Hi K. Pong

Try:

with ThisWorkbook.Sheets("Criteria")
..Range("A1") = group ' Function exits at this line worksheet
..Range("B1") = status

end with

You could also try putting on error resume next at thestart of the sub.

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"pongthai" wrote:

Hi All,

I'm writing a function that counts the number of rows that match a composite
criteria. A set of criteria is passed in the function's arguments. See
example:

Function CountCustomers(group as string, status as string) as Integer
dim cr as Range ' Criteria Range
dim Result as Integer

Result = 0

set r = ThisWorkbook.Sheets("Criteria").Range("A1:B2")
r.Range("A1") = group ' Function exits at this line when used in
worksheet
r.Range("B1") = status

For Each s In ThisWorkbook.Sheets
If InStr(s.Name, "CUST")=1 Then
result = result +
WorkSheetFunction.DCountA(s.Range("M8:M11"), "Name", criteriaRange)
End If
Next
CountCustomers = result
End Function

I created a hidden sheet "Criteria" to be used as a selection criteria for
the DCountA function (last argument).

But when I use "=CountCustomer" in the Excel sheet cell, it appeared as
'#Value'. I know that this happens when we try to modify cells in a function.

Is there any way to make the calculation go through without premature
exiting at the remarked point?

Thanks a lot in advance
pongthai

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Using dcount function within user-defined worksheet function

User defined functions are not allowed to modify other cells.

Charles Williams
Decision Models

"pongthai" wrote in message
...
Hi All,

I'm writing a function that counts the number of rows that match a
composite
criteria. A set of criteria is passed in the function's arguments. See
example:

Function CountCustomers(group as string, status as string) as Integer
dim cr as Range ' Criteria Range
dim Result as Integer

Result = 0

set r = ThisWorkbook.Sheets("Criteria").Range("A1:B2")
r.Range("A1") = group ' Function exits at this line when used in
worksheet
r.Range("B1") = status

For Each s In ThisWorkbook.Sheets
If InStr(s.Name, "CUST")=1 Then
result = result +
WorkSheetFunction.DCountA(s.Range("M8:M11"), "Name", criteriaRange)
End If
Next
CountCustomers = result
End Function

I created a hidden sheet "Criteria" to be used as a selection criteria for
the DCountA function (last argument).

But when I use "=CountCustomer" in the Excel sheet cell, it appeared as
'#Value'. I know that this happens when we try to modify cells in a
function.

Is there any way to make the calculation go through without premature
exiting at the remarked point?

Thanks a lot in advance
pongthai



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Using dcount function within user-defined worksheet function

You really should use Option Explicit in your code, you are declaring one
variable and then trying to use two others in its place, won't work.

This works

Function CountCustomers(group As String, status As String) As Integer
Dim cr As Range ' Criteria Range
Dim Result As Integer

Result = 0

Set cr = ThisWorkbook.Sheets("Criteria").Range("A1:A2")
cr.Range("A1") = group ' Function exits at this line when used in
Worksheet
cr.Range("A2") = status

For Each s In ThisWorkbook.Sheets
If InStr(s.Name, "CUST") = 1 Then
Result = Result + _
Application.WorksheetFunction.DCountA(s.Range("M8: M11"), "Name", cr)
End If
Next
CountCustomers = Result
End Function



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"pongthai" wrote in message
...
Hi All,

I'm writing a function that counts the number of rows that match a

composite
criteria. A set of criteria is passed in the function's arguments. See
example:

Function CountCustomers(group as string, status as string) as Integer
dim cr as Range ' Criteria Range
dim Result as Integer

Result = 0

set r = ThisWorkbook.Sheets("Criteria").Range("A1:B2")
r.Range("A1") = group ' Function exits at this line when used in
worksheet
r.Range("B1") = status

For Each s In ThisWorkbook.Sheets
If InStr(s.Name, "CUST")=1 Then
result = result +
WorkSheetFunction.DCountA(s.Range("M8:M11"), "Name", criteriaRange)
End If
Next
CountCustomers = result
End Function

I created a hidden sheet "Criteria" to be used as a selection criteria for
the DCountA function (last argument).

But when I use "=CountCustomer" in the Excel sheet cell, it appeared as
'#Value'. I know that this happens when we try to modify cells in a

function.

Is there any way to make the calculation go through without premature
exiting at the remarked point?

Thanks a lot in advance
pongthai



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
User defined function returns imprecise value when used in worksheet JohnM[_3_] Excel Discussion (Misc queries) 3 December 1st 09 12:52 PM
User Defined Function use any worksheet Babylynn Excel Discussion (Misc queries) 2 April 1st 09 06:23 PM
Excel - User Defined Function Error: This function takes no argume BruceInCalgary Excel Programming 3 August 23rd 06 08:53 PM
Error messages from user defined Excel worksheet function Tom Ogilvy Excel Programming 3 March 18th 06 04:30 PM
Automatic calculation of user-defined worksheet function Bart Deschoolmeester Excel Programming 2 October 8th 03 04:37 PM


All times are GMT +1. The time now is 09:39 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"