How to use a public function in a similar way as worksheetfunction in VBA?

etropal

New Member
Joined
Jan 6, 2013
Messages
2
I’m writing a very large VBA macro to analyze data from multiple worksheets. All my functions are used in the following form (example):
ReDim TotVAKWNAArray(1 To 5)
For bCount = 1 To 5
TotVAKWNAArray(bCount) = Application.WorksheetFunction.CountIfs(Range("D:D"), VAKW, Range("AW:AW"), "1", Range("N:N"), ToerReg, Range("K:K"), Prov, Range("W:W"), Gem)
If ActiveSheet.Index = 5 Then
Worksheets(1).Select
Else
ActiveSheet.Next.Select
End If
Next bCount

Now, at the end of a laborious process, I need to use a function in the macro, that is not a standard Excel worksheet function (counting conditional unique values). I managed to find a public function that does the trick perfectly (here called UniqueCountv4). If I try to use this like in the example things go wrong. Is there anyone who knows how to fix this problem? I would be REALLY grateful I anyone could provide me with a solution.

ReDim TotVAKLArray(1 To 5)
For bCount = 1 To 5
TotVAKLArray(bCount) = Application.WorksheetFunction.UniqueCountv4(Range("B:B"), Range("D:D"), VAKL) ', Range("N:N"), ToerReg, Range("K:K"), Prov, Range("W:W"), Gem)
If ActiveSheet.Index = 5 Then
Worksheets(1).Select
Else
ActiveSheet.Next.Select
End If
Next bCount
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If the UniqueCountv4 Functio is reacheable from your code then you should be able to call it like this :

Code:
TotVAKLArray(bCount) = [COLOR=#ff0000]UniqueCountv4[/COLOR](Range("B:B"), Range("D:D"), VAKL)
You can't use the Application.WorksheetFunction qualifiyer because it is not part of the native worksheet functions.
 
Upvote 0
Many thanks Jaafar!


Even though I had already tried your solution earlier my results always turned out wrong, so I went looking for answers in all the wrong places. Thanks to your kind reply I located the real problem; namely the fact that my variable VAKL was defined as a String instead of Variant which caused the problem with the public function...
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top