VBA excel Passing back parameter - vba

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

Related

Getting a specific Settings in Sub and manipulating it

I want a sub which gets a already existing variable from My.Settings and save it with NewValue variable. The example below shows what I need, but the problem is My.Settings.a is not from a Settings property, but a String one.
Private Sub Testing()
ThisSettings(My.Settings.a, "123")
ThisSettings(My.Settings.b, "456")
ThisSettings(My.Settings.c, "789")
End Sub
Private Sub ThisSettings(Sett As Settings, NewValue As String)
Sett = NewValue
My.Settings.Save()
End Sub

How do I exit sub where function is used?

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

Passing a parameter to a function to get a string back

I think this is relatively a simple question but it seems like I've been having a small issue with this...
I have a form where I try to pass a parameter to a function and get a string back in return with a value
ParmValue = "Juarez"
dim CheckValue = GetValueFromFunction(parmValue)
Private Sub GetValueFromFunction (ByVal Optional ValueFromFunc as string)
'do stuff
getValueFromFunction = "OK"
or
getValueFromFuncton="NOTOK"
return GetValueFromFunction
End sub
Basically I'm looking to get a string value back from my GetValueFromFunction so that CheckValue holds that value. What am I doing wrong?
You can't return something from a Sub; it has to be a Function:
Private Function GetValueFromFunction(Optional ValueFromFunc as String = Nothing) As String
Dim returnValue As String
'do stuff...
If someCondition Then
returnValue = "OK"
Else
returnValue = "NOTOK"
End If
Return returnValue
End Function
A Function is a method that returns something, whereas a Sub is a method that doesn't return something (it's more like an action). In C#, a Sub would return void.
Because you are returning a value, you need to use a Function. Your code is wrong because it declares a Sub (procedure).
The correct use of a Function is:
Private Function GetValueFromFunction (ByVal ValueFromFunc As String) As String
' do stuff
GetValueFromFunction = "OK"
' or
GetValueFromFunction = "NOTOK"
Return GetValueFromFunction
End Function
Note the use of As String. This is to dictate the return type of your Function.

Calling a Sub and returning a value

This might seem like an insanely easy question, but I can't seem to find an answer anywhere to it. I'd like to think that I am decent at VB but while I was learning javascript the other day I found something that seemed awesome and now I can't figure out how to do it in VB.
In javascript it looks like this:
var someValue = getThatValue()
It's both calling and setting the value from the getThatValue() sub. what is the VB equivalent?
Edit
I've tried doing this:
private sub main()
dim value = getValue()
'do something with value
end sub
private sub getValue()
return 3
end sub
That doesn't seem to work, how can I get that to work?
Private Sub Main()
Dim value = getValue()
'do something with value
End Sub
Private Function getValue() As Integer
Return 3
End Function
You should be using a Property:
Private _myValue As String
Public Property MyValue As String
Get
Return _myValue
End Get
Set(value As String)
_myValue = value
End Set
End Property
Then use it like so:
MyValue = "Hello"
Console.write(MyValue)
Sub don't return values and functions don't have side effects.
Sometimes you want both side effect and return value.
This is easy to be done once you know that VBA passes arguments by default by reference so you can write your code in this way:
Sub getValue(retValue as Long)
...
retValue = 42
End SUb
Sub Main()
Dim retValue As Long
getValue retValue
...
End SUb

How to get functions to not evaluate the return until the end

I have functions in my program that stop after it reaches a Return line but I need the function to continue in case what I want the function to return changes.
Is there a workaround for this or a way to force the function to continue after a return?
I made a Test Program to demonstrate the problem. It is a Form that contains only a button.
Here is the source code:
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
If testFunction() = True Then
MsgBox("It returned True!")
Else
MsgBox("It returned False!")
End If
End Sub
Function testFunction() As Boolean
Dim testVariable As Boolean = True
Return False
If testVariable = True Then
Return True
End If
End Function
The messagebox always says "It returned False" when if it continued going through the code like I want it to it would have returned true.
You already have a flag variable set up, so use it!
Function testFunction() As Boolean
Dim testVariable As Boolean = True
testVariable = False
Return testVariable
End Function
Also, don't ever use = True or = False in comparisons. = True is 100% redundant and x = False should be Not x.
EDIT: Sorry, that wasn't clear.
If x = True Then
' ...
End If
is always* the same as
If x Then
' ...
End If
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
If testFunction() Then
MsgBox("It returned True!")
Else
MsgBox("It returned False!")
End If
End Sub
Function testFunction() As Boolean
Dim testVariable As Boolean = True
If testVariable Then
Return True
End If
Return False
End Function
You could also do:
Function testFunction() As Boolean
Dim testVariable As Boolean = True
Return testVariable
End Function
Since testVariable is a boolean and the return type is boolean.