ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Correct Syntax for Calling other Subs (https://www.excelbanter.com/excel-programming/332098-correct-syntax-calling-other-subs.html)

Andibevan[_2_]

Correct Syntax for Calling other Subs
 
Hi All,

What do I need to change in order to get the following to work. Should I be
using a function for this sort of routine?

I get the error "expected function or variable"

Sub TestSub(var1 As Integer)

TestSub = var1 + 2

End Sub

Sub testsub2()
Dim var1

var1 = TestSub(2)
MsgBox var1

End Sub



K Dales[_2_]

Correct Syntax for Calling other Subs
 
When your code returns a value it needs to be a Function, not a Sub:
Function TestSub(var1 As Integer) as Integer

TestSub = var1 + 2

End Function

Sub testsub2()
Dim var1

var1 = TestSub(2)
MsgBox var1

End Sub

"Andibevan" wrote:

Hi All,

What do I need to change in order to get the following to work. Should I be
using a function for this sort of routine?

I get the error "expected function or variable"

Sub TestSub(var1 As Integer)

TestSub = var1 + 2

End Sub

Sub testsub2()
Dim var1

var1 = TestSub(2)
MsgBox var1

End Sub




Andibevan[_2_]

Correct Syntax for Calling other Subs
 
Thanks - didn't realise that

"K Dales" wrote in message
...
When your code returns a value it needs to be a Function, not a Sub:
Function TestSub(var1 As Integer) as Integer

TestSub = var1 + 2

End Function

Sub testsub2()
Dim var1

var1 = TestSub(2)
MsgBox var1

End Sub

"Andibevan" wrote:

Hi All,

What do I need to change in order to get the following to work. Should I

be
using a function for this sort of routine?

I get the error "expected function or variable"

Sub TestSub(var1 As Integer)

TestSub = var1 + 2

End Sub

Sub testsub2()
Dim var1

var1 = TestSub(2)
MsgBox var1

End Sub







All times are GMT +1. The time now is 05:11 PM.

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