Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:33:04+00:00 2026-06-07T17:33:04+00:00

I have a function that I want to call from a variety of modules.

  • 0

I have a function that I want to call from a variety of modules. Whats the best way to do this in VB (excel).

module “SheetExists”

Function Name(SheetName As String) As Boolean
' returns TRUE if the sheet exists in the active workbook
    SheetExists = False
    On Error GoTo NoSuchSheet
    If Len(Sheets(SheetName).Name) > 0 Then
        SheetExists = True
        Exit Function
    End If
NoSuchSheet:
End Function

module “Main”

If Not SheetExists.Name("mySheet") Then
    'do this
Else
    ' else do this
End If

I DONT want to have to do this or do I??

Call SheetExists.Name("mySheet")

Is that the only way to call a function from another module? Do I have to declare it as a Public function or something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:33:07+00:00Added an answer on June 7, 2026 at 5:33 pm

    No, you don’t have to do that, and you can call your function from anywhere.

    Try this:

    Put this code in Module1:

    Sub TestSheetExists()
        If SheetExists("Sheet1") Then
            MsgBox "I exist!"
        End If
    End Sub
    

    And this in Module2:

    Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
        Dim sht As Worksheet
    
         If wb Is Nothing Then Set wb = ThisWorkbook
         On Error Resume Next
         Set sht = wb.Sheets(shtName)
         On Error GoTo 0
         SheetExists = Not sht Is Nothing
     End Function
    

    Obviously you can use whatever names for your modules you want.


    EDIT: I see that calling from different modules still isn’t working for you. Follow these steps exactly to set up a test workbook that should help you understand the problem.

    1. Create a new Excel workbook
    2. Open the VBA Editor (Alt-F11)
    3. Right-click on the project and select insert module. Repeat this 4x to get 4 modules.
    4. Press F4 to open the properties window, if it isn’t already open
    5. Change your module names to the following: CallMe, CallMeAgain, CallMeBack, Validation
      Renamed modules
    6. In the Validation module, paste the following function:

      Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
          Dim sht As Worksheet
      
           If wb Is Nothing Then Set wb = ThisWorkbook
           On Error Resume Next
           Set sht = wb.Sheets(shtName)
           On Error GoTo 0
           SheetExists = Not sht Is Nothing
      End Function
      
    7. Paste this sub into CallMe:

      Sub TestSheetExistsFromCallMe()
          If SheetExists("Sheet1") Then
              MsgBox "I exist, and I was called from CallMe!"
          End If
      End Sub
      
    8. Paste this into CallMeBack:

      Sub TestSheetExistsFromCallMeBack()
          If SheetExists("Sheet1") Then
              MsgBox "I exist, and I was called from CallMeBack!"
          End If
      End Sub
      
    9. Paste this into CallMeAgain:

      Sub TestSheetExistsFromCallMeAgain()
          If SheetExists("Sheet1") Then
              MsgBox "I exist, and I was called from CallMeAgain!"
          End If
      End Sub
      
    10. Press F5 to run the code from within CallMe. You should see the following messagebox:
      enter image description here

    11. Run the code from any of the 3 “Call” modules and you should see the corresponding messagebox.

    I got the SheetExists function from Tim Williams (https://stackoverflow.com/a/6688482/138938) and use it all the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I want to call from within a class method.
I have the following legacy VB6 function that I want to call from C#.
Say I have a function foo that I want to call n times. In
I have function getCartItems in cart.js and I want to call that function in
I have a function fun that returns a double. I want to call the
I have a function in R that I call multiple times. I want to
I want to simply call one function that is defined in BroadClass Activity from
Say I have a function in JS function playerJob(job){} I call this method from
I have two JavaScript functions that I want to call from inside of a
I have a function like this that does an ajax call to grab some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.