Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I return in vba?(in c# we use return)

VB
Private Sub Command54_Click()
If IsNull(CName.Value)
MsgBox ("please fill the blank")
return//how should i return?
Else
MsgBox ("accepted")
End Sub


I meant to stop the Sub or exit from it! not actually return something
Posted
Updated 8-Apr-12 18:35pm
v4

The keyword you need is "Exit Sub" and nothing else. It works like return in a void method; likewise, it is not required (in contrast to functions), as the execution will be returned from Sub after execution of its last statement.

—SA
 
Share this answer
 
If you would like to return some values, you need to use function, like this:
VB
Sub Test()
    Msgbox "1*2=" &  Twice(1), vbInformation, "Information..."
    Msgbox "5*2=" &  Twice(5), vbInformation, "Information..."
    Msgbox "13*2=" &  Twice(13), vbInformation, "Information..."
End Sub

Function Twice(myVal as Integer) As Integer
    Twice = myVal * 2 
End Function


...but if you would like to return into some instruction inside code block, you need to write some subprogram, like this:
VB
Sub BlaBlaBla()
'some instructions
'...
'subprogram
'here we can return with some conditions 
AnotherOneCheck:
'another instructions
'...
'later...
If some_condition = some_value Then
    GoTo AnotherOneCheck
End If

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900