banner



How To Use Return Key Word In Vba Excel

Excel VBA Render Statement

VBA is wonderful and a lifesaver for those who know the in and out of VBA coding VBA lawmaking refers to a set up of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task. read more than . It tin can save tons of time at our workplace. In our earlier articles, we have discussed many things and concepts of VBA return. One such concept is the GoSub Return statement. In this article, nosotros will introduce you to these concepts.

VBA-Return

Yous are gratis to use this image on your website, templates etc, Delight provide us with an attribution link Commodity Link to be Hyperlinked
For eg:
Source: VBA Render (wallstreetmojo.com)

What is GoSub Return Statement?

The statement "Go Sub" means information technology will become to the line of lawmaking indicated by characterization through proper noun and performs a specific set of the chore until it finds the argument "Render."

This is similar to the error handler argument "GoTo Label," but what "GoSub Return" does is information technology will return back to the next line of lawmaking rather than continuing from the label name.

Below is the syntax of the VBA GoSub Return statement.

                          GoSub              [Characterization Name]            ….. Line of Code            [Label Proper noun]:            ….. Line of Code to perform the job          

I know it is difficult to understand from reading the syntax only using the instance, you lot may be able to sympathise the concept.

Before I get-to examples, permit me tell a few things about this statement.

  • GoSub statement calls subroutine in VBA SUB in VBA is a procedure which contains all the code which automatically gives the statement of cease sub and the heart portion is used for coding. Sub statement can be both public and private and the proper noun of the subprocedure is mandatory in VBA. read more , which is named past a label within the same routine or Role.
  • Both GoSub and Return should be within the same procedure. We cannot call another subroutine macro here.
  • You lot can introduce any number of GoSub Return statements.
  • A return statement will continue the execution of the code from where it left off earlier it jumped to the subprocedure indicated by a label name.

How to utilise GoSub Return Argument in VBA?

Y'all can download this VBA Render Excel Template hither – VBA Render Excel Template

Example #1

To understand the usage of this statement, look at the beneath code first. Subsequently I will explain to you the code line by line.

Code:

            Sub            Go_Sub_Return()                          GoSub            Macro1            'Label Name1                          GoSub            Macro2            'Characterization Name2                          GoSub            Macro3            'Label Name3                          Exit Sub                        Macro1:   MsgBox "Now running Macro1"            Return            Macro2:   MsgBox "Now running Macro2"            Return            Macro3:   MsgBox "Now running Macro3"            Return                        End Sub          

To empathise this lawmaking, let's run the code line past line by pressing the F8 key. Upon pressing the F8 fundamental first time, it will initiate the macro running.

VBA Return Example 1

Now press the F8 key one more time to go to the next line.

VBA Return Example 1-1

We all know macro will run the code line by line, merely if yous press the F8 key, information technology will non go to the next line; rather, it works differently.

VBA Return Example 1-2

It has jumped to the label named as "Macro1" this is because in the previous statement "GoSub," we have instructed the macro to become to the subprocedure label name "Macro1", and then accordingly, it has jumped to the respective label proper noun.

Now by pressing the F8 key, excel macro A macro in excel is a series of instructions in the form of lawmaking that helps automate transmission tasks, thereby saving fourth dimension. Excel executes those instructions in a stride-by-step manner on the given data. For example, it tin exist used to automate repetitive tasks such as summation, jail cell formatting, information copying, etc. thereby quickly replacing repetitious operations with a few clicks. read more will execute the characterization "Macro1" task of showing the result in the message box.

VBA Return Example 1-3

Click on the Ok of the bulletin box to return to the coding window.

VBA Return Example 1-4

Now it had highlighted the "Return" statement. If you press the F8 key one more than time, what it volition do is it will "Return Back" to the previous line of code before information technology jumped to the label name.

VBA Return Example 1-5

Last time information technology executed the code "GoSub Macro1," and it performed the label name "Macro1" job. Since nosotros have mentioned the "Return" statement, it is dorsum to the next line of code i.eastward.

"GoSub Macro2"

What is statement says is, "go-to label named Macro2". In the beneath for label "Macro2", I take mentioned a specific ready of tasks.

VBA Return Example 1-6

Now we volition get the second subprocedure chore of showing value in the bulletin box.

VBA Return Example 1-7

Now click on, Ok. It will highlight the second "Return" statement.

VBA Return Example 1-8

Now upon clicking the F8 key, it will render to the side by side line of code before it returns to the label "Macro2". At present information technology will highlight "GoSub Macro3".

VBA Return Example 1-9

Now it volition go to the label "Macro3", printing the F8 key.

Example 1-10

Now third label job will exist executed.

Example 1-11

At present click on, OK. It will highlight the "Return" statement.

Example 1-12

At present press the F8 key one more fourth dimension; it volition jump back to the line of code "Exit Sub."

Example 1-13

No, it volition exit the subroutine. Information technology is necessary to add together the give-and-take "Exit Sub" otherwise, and it will encounter an fault.

Example #2

Now take a await at one more instance of using the GoSub Return statement.

Code:

            Sub            Go_Sub_Return1()                          Dim            Num            As Long            Num = Awarding.InputBox   (Prompt:="Delight enter the number here", Title:="Divsion Number")                          If            Num > 10            Then                          GoSub            Segmentation                          Else            MsgBox "Number is less than x"                          Get out Sub                          End If                          Get out Sub            Division:   MsgBox Num / 5            Return                        End Sub          

This code will ask you to enter the number which is >x. If the number is >10, and then it will perform the chore of dividing it by v (Label Name is "Division"), or else it will show the message every bit "Number is less than ten".

VBA Return Example 2

Things to Remember

  • The label proper name should be the same in GoSub and Return statement, and in the return argument label name should be followed by a colon (:).
  • Always use Exit Sub in VBA later all the return statements are made to avert the error message.

Recommended Manufactures

This has been a guide to VBA Return. Here we learn how to use GoSub Render Argument in VBA forth with examples and a downloadable excel template. Beneath are some useful excel articles related to VBA –

  • VBA IsDate
  • VBA VARTYPE Examples
  • Switch in VBA
  • Filter in VBA

How To Use Return Key Word In Vba Excel,

Source: https://www.wallstreetmojo.com/vba-return/

Posted by: leavittworsoll.blogspot.com

0 Response to "How To Use Return Key Word In Vba Excel"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel