Senin, 24 Oktober 2016

Re: [ExcelVBA] failing function


You can't run a function in the VBA editor that requires variables to be passed.
you COULD create a test sub that CALLS the function.

in "standard" module "Module1", I created a simple function:
Function Func(ByVal x As Range, y As Range)
    Func = x.Count
End Function

In the worksheet,
I put A,B,C,D,E in row 1
and 1,2,3,4,5 in row 2

In cell A4 I put: =FUNC(A1:E1,A2:E2)

It returned (5) in cell A4.

So... my assumption is that you have an error somewhere in your code within the function.

"Do all the good you can,
By all the means you can,
In all the ways you can,
In all the places you can,
At all the times you can,
To all the people you can,
As long as ever you can." - John Wesley

On Sunday, October 23, 2016 6:54 AM, "Green [ExcelVBA]" <> wrote:

Nice answer Derek!!!

-----Original Message-----
From: Derek Turner [ExcelVBA] <>
To: ExcelVBA <>
Sent: Sat, Oct 22, 2016 12:52 pm
Subject: Re: [ExcelVBA] failing function

Dear Greg

This worked for me :-

Public Function Func(x As Range, y As Range) As Variant
' ---- my code ----
    Func = "My result"
End Function

In the spreadsheet I put :-
=PERSONAL.XLSB!Func( A5:E5,A6:E6), -  i.e. the full path to the Function.

You cannot directly run a Function in the VBA IDE, you have to call it from a Sub. That is why it asks for a Macro name.

You don't need the ByVal unless you are modifying the ranges in your code. ByRef is implicit.

Returning a Variant type to the spreadsheet makes it more resilient while you are developing.


Derek Turner

From: "'David Smart' [ExcelVBA]" <>
Sent: Friday, 21 October 2016, 23:50
Subject: Re: [ExcelVBA] failing function


"Public"?  I.e.
Public Function Func(ByVal x as range, y as range) as variant
Not sure about the ByVal on the range, either.  You might need ByRef on both parameters.  As in
Public Function Func(ByRef x as range, ByRef y as range) as variant
Lastly, avoid a return type of Variant if you can - give it a more appropriate return type (unless, of course, you are returning various types).

Regards, Dave S
----- Original Message -----
Sent: Saturday, October 22, 2016 1:09 AM
Subject: [ExcelVBA] failing function

I am trying to create a Function

I create a module (module1 in General Declaration)

I put in the code
Function Func(ByVal x as range, y as range) as variant
---- my code ----
Function End

in the spreadsheet I put in   =Func(A1:E1,A2:E2)

The results are VALUE error
I try to run the Function in VBA Editor and it keeps asking for a Macro Name.

I am missing something simple I am sure, but what is it?

Thanks for responding to this.


Posted by: Paul Schreiner <>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.

Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: to enter the ezine, then search the ARCHIVES for EXCEL VBA.

Visit our ExcelVBA group home page for more info and support files:

More free tutorials and resources available at:




Tidak ada komentar:

Posting Komentar