ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Call a Function (https://www.excelbanter.com/excel-programming/355933-call-function.html)

Desert Piranha[_65_]

Call a Function
 

Hi all,

I can call a Macro like

'Call TestCode
or
'Run TestCode
or
TestCode
or use Select Case
etc

If i have a function named TestCode why can i not call it in this way?


--
Desert Piranha


------------------------------------------------------------------------
Desert Piranha's Profile: http://www.excelforum.com/member.php...o&userid=28934
View this thread: http://www.excelforum.com/showthread...hreadid=522044


mudraker[_346_]

Call a Function
 

Desert

You can call the function the same way unless you are expecting the
function to return data back to your macro or if you are passing
variable data onto the function

Sub Macro1()
call Function1
End Sub

Function MyFunction1()
function code here
End Function

or
Sub Macro2()
dim MyVariable As String
MyVariable = MyFunction2
End Sub

Function MyFunction2() as String
MyFunction2 = "Hi"
End Function

or

Sub Macro3()
dim MyVariable As Integer
MyVariable = MyFunction3(4)
End Sub

Function MyFunction3(iVariable As Integer) as Integer
MyFunction3 = iVariable + 6
End Function

or

If the function is on a worksheet module then you will need to use the
full sheet name as well as the function name

eg
call Sheet1.Function1


--
mudraker
------------------------------------------------------------------------
mudraker's Profile: http://www.excelforum.com/member.php...fo&userid=2473
View this thread: http://www.excelforum.com/showthread...hreadid=522044


Norman Jones

Call a Function
 
Hi Dave,

If i have a function named TestCode why can i not call it in this way?


You can. However, if the function returns a value, you may not observe any
response.

For demonstaryion purposes, I include a msgbox in the function:


Try:

'=============
Public Function ABC(aVal As Double)
ABC = aVal * 2
MsgBox ABC
End Function
'<<=============

'=============
Sub TestIt()
ABC (5)
Call ABC(100)
End Sub
'<<=============


---
Regards,
Norman



"Desert Piranha"
<Desert.Piranha.24n3vm_1142306702.1674@excelforu m-nospam.com wrote in
message news:Desert.Piranha.24n3vm_1142306702.1674@excelfo rum-nospam.com...

Hi all,

I can call a Macro like

'Call TestCode
or
'Run TestCode
or
TestCode
or use Select Case
etc

If i have a function named TestCode why can i not call it in this way?


--
Desert Piranha


------------------------------------------------------------------------
Desert Piranha's Profile:
http://www.excelforum.com/member.php...o&userid=28934
View this thread: http://www.excelforum.com/showthread...hreadid=522044




Desert Piranha[_66_]

Call a Function
 

Hi Mudraker & Norman,

Thank You both for contributing to this.

This is over my head. Im quiting on this for now, as i can't seem to
work with it.
Its for personal use, and i have some stuff to do for work.
I will keep your stuff for future.

Thx again Norman and Mudraker


--
Desert Piranha


------------------------------------------------------------------------
Desert Piranha's Profile: http://www.excelforum.com/member.php...o&userid=28934
View this thread: http://www.excelforum.com/showthread...hreadid=522044



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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com