By using expertatexcel.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

Become an Σxpert at Σxcel.com

 


Sub - End Sub

Sub is short for Subroutine.

What is a subroutine in VBA?

A subroutine is like a habit. Say the habit you're trying to learn is to put away things, but you don't do it. After practicing it your brain does it without thinking. This is like a subroutine a routine thing to do in your subconscious.

To start the subroutine we use the following terms:
  • Call
  • Invoke
  • Run


    which all means the same thing. The subroutine does it's work.

    A subroutine is a piece of programming that does a task for you.

    Any time you record a macro you create a new subroutine.

    Subroutines are a way of writing the code once and reusing it. For example here is a subroutine to Create a Sheet. Once I get this working, any time I want to create a sheet, I can just "call" the subroutine to do the work of creating a sheet.




    Function CreateSheet(sh As String)
    'Version: 1.002
    'Purpose: To Create a sheet for passed name and delete the sheet if it already exists
    On Error Resume Next
    CreateSheet = ""
    Worksheets(sh).Visible = True
    On Error Resume Next
    Worksheets(sh).Select
    If Err = 9 Or Err = -2147352565 Then
        'nothing to do sheet doesn't exist
    ElseIf Err = 0 Then
        'Delete the sheet
        If ActiveWindow.SelectedSheets.Count > 1 Then
             MsgBox "Something went wrong. Multiple sheets were selected."
        Else
            Application.DisplayAlerts = False
            
            ActiveWindow.SelectedSheets.Delete '@ this is worrysome
            Application.DisplayAlerts = True
        End If
        
    Else
        CreateSheet = "Unexpected error during CreateSheet = " & Err & " " & Error
        
    End If
    On Error Resume Next

    Sheets.Add.Name = sh
    If ActiveSheet.Name <> sh Then
        'There may be a problem with Excel
        CreateSheet = "Excel is behaving Strangely. Added sheet but not added. Usually do to resource issue"
        Exit Function
    End If

        Sheets(sh).Move After:=Sheets(Sheets.Count)

    End Function



      Examples of things that are like subroutines
    • Hyperlink - You click read the info and go back to where you came
    • Reading a book and it says see appendix A. You go to the
    • Reading a book and you don't understand a term so you look it up
    • Recipe that says see section x for chopping

      Why use Subroutines?
    • Reusable
    • Removes duplicate code
    • Solves duplicate issue
    • Can be worked on independently

      Example of a subroutine
      Sub CalculateTax()

      Get Items price
      Multiply price by tax rate
      Print out the tax

      End Sub


      Details
      code runs
      ...
      ...
      Calls Subroutine ------>    Sub CalcuateTax()
      ...  <------------+
      ....              |
      ...               |             ...
                        |             ...
                        |              ...
                        +-------- End Sub


      Some things similar to a subroutine are:
    • Functions
    • Webservice
    • Object.Method

      which we'll learn more about in later lessons.

    •