How do I exit sub where function is used? - vba

Was wondering if it was possible to exit sub using a function :
For example
Public Function test()
Exit Sub 'does not work
End Function
Sub MySub()
test() 'Here we should Exit MySub()
End Sub
But this Trigger an error. Is there a way to do this ?
NOTE I don't want to exit the function but I would like to exit the Sub or Stop the code execution directly into my function.

That is not possible. However, if your intent is to have test decide whether or not to abort MySub, then just return a boolean from test and act on it in MySub:
Sub MySub()
If test() Then
Exit Sub
End If
End Sub

From MSDN:
Exit Function Immediately exits the Function procedure in which it
appears.
You should use Exit Function and not Exit Sub inside test()
Update:
If you wish to exit the calling sub MySub, you should return some value from the test function. For Example:
Public Function test()
test = False
End Function
Sub MySub()
If test() = False Then
Exit Sub
End If
End Sub

Try this approach, please. Your function must return something in order to exit the sub. Your sub is not automatically exited if the function is. You must set a condition inside the sub to exit it in case of a specific function return:
Public Function test(a As Long, b As Long) As Boolean
If a + b > 10 Then
test = True
Else
Exit Function ' even if it is not necessary
End If
End Function
Sub MySub()
If Not test(5, 7) Then Exit Sub
'do whatever...
End Sub

Related

How to end one procedure (Sub, Function etc) from another in VBA?

I want to end a main sub from another sub or function.
Here is an example code to illustrate what I need to do:
Sub main()
Call endMainSub
'do other stuff
End Sub
Sub endMainSub()
'here I need a code to break main Sub
End Sub
From endMainSub, I would like to terminate main sub before "do other stuff".
You should use a function for that.
Sub main()
If Not endMainSub Then
'do other stuff
End If
End Sub
Private Function endMainSub() As Boolean
Dim Fun As Boolean ' function return value
' break if A3 = "No" OR C3 > 10
Fun = (Cells(3, "A").Value = "No")
If Not Fun Then
Fun = (Cells(3, "C").Value > 10)
End If
endMainSub = Fun
End Function
Make the function Private if you aren't going to call it from another module. Remove the Private to make it Public by default.
End Finishes all code execution. Alternatively, if you need more control, have a global flag (a boolean variable), based on which execution will continue or not.

Call module with multiple inputs

I've tried finding a solution to this problem but have been unable to.
In generic form:
Module1
Sub Source()
Call Module2.Run
End Sub
Module2
Sub Run()
Value = 10
Some code which uses Value as input
End Sub
What I want to be able to do is to be able to define multiple Values in Module1 and then run Module2.Run() with each value.
Module1
Sub Source()
Value = 10, 20, 30
Call Module2.Run (10)
Call Module2.Run (20)
Call Module2.Run (30)
End Sub
Module2
Sub Run()
Value = Input from Module1.Source()
Some code which uses Value as input
End Sub
Or something along these lines. Any input would be greatly appreciated.
You can pass parameters through arguments like so
Sub Sub1 ()
Dim myVal as Long
myVal = 1000
Sub2 (myVal) 'The "Call" is not necessary
End Sub
Sub Sub2 (myVal as Long) 'This sub requires an input to run (myVal)
MsgBox myVal
End Sub
You can create an array, fill it and pass as an argument.
Avoid using names like Source and Run which are already used by Excel;.
Option Explicit
Sub Sour()
Dim arr_1d() As Variant
arr_1d = Array("val1", "val2", "val3")
Dest arr_1d
End Sub
Sub Dest(arr_1d() As Variant)
Dim y As Long
For y = LBound(arr_1d) To UBound(arr_1d)
Debug.Print arr_1d(y)
Next
End Sub

How to exit a main sub within a called sub

I have 2 subs that look like this:
How can i stop my "main" sub if my statement is true in the sub i just called?
Public Sub main()
call run()
more code....
End Sub
Public Sub run()
If ProgressBar1.Value = 100 Then
(i want to stop the code running if this statement is true, it shall not continue in my main sub.)
End If
End Sub
You can't do it if Run() is a Sub. You need your Run() as a Function. return a Boolean indicating if you want to stop main, and call in inside an If:
Public Function run() As Boolean
If ProgressBar1.Value = 100 Then
Return True
End If
' some more code if needed
Return False
End Function
Then call it in your main sub like this:
Public Sub main()
' some main code here
If Run() Then
Return
End If
' The rest of main code here
End Sub

VBA excel Passing back parameter

Okay I want to add something to this macro
Sub Search()
Inputbox myInput
found = false
loop
Call getInput (myInput) '~> check multiple files
end loop
If found = false
'DO something
End if
End sub
Sub getInput(ByVal inputVar As String, ByVal Input as Boolean)
If a = inputVar Then
found = true '~> I want to pass this parameter back to search
End If
End sub
The case is like, I want my sub to pass the found parameter from
Search() to getInput() and then getInput() return the found parameter to
Search()
Should I add something like search(ByVal found as boolean) ?
if you want to return a value, then you should change the getInput Sub to a function as they can return values.
Sub Search()
Dim found As Boolean
InputBox myInput
found = checkInput(myInput)
If found = False Then
'DO something
End If
End Sub
Function checkInput(inputVar As String) As Boolean
Dim result As Boolean
'do your checking here and set result
'return the result
checkInput = result
End Function

Using a sub var into other sub visual basic

How can i get a var value from a sub by calling it in other sub ? like
Sub test()
Dim a As Integer
a = 1
End Sub
Sub testshow()
MessageBox.Show(test.a)
End Sub
In VBA (which your tag states) you need to change Sub into Function which will be:
Function test()
Dim a As Integer
a = 1
test = a
End Function
Sub testshow()
MsgBox test
End Sub
EDIT after comment: If you using more then one variable then:
Function test(whichVar)
Dim a As Integer
If whichVar = 1 then
a = 100
ElseIf whichVar = 2 Then
a = 200
'etc...
End if
test = a
End Function
Sub testshow()
MsgBox test(2) 'will give you 200
End Sub