Results 1 to 11 of 11

How does one use a VB module in an Excel sheet's code?

Threaded View

Sarvazad How does one use a VB module... 02-27-2013, 05:01 PM
JosephP Re: How does one use a VB... 02-27-2013, 05:17 PM
xlbiznes Re: How does one use a VB... 02-27-2013, 05:21 PM
Sarvazad Re: How does one use a VB... 02-27-2013, 05:27 PM
Sarvazad Re: How does one use a VB... 02-27-2013, 05:52 PM
JosephP Re: How does one use a VB... 02-27-2013, 05:55 PM
Sarvazad Re: How does one use a VB... 02-27-2013, 06:06 PM
JosephP Re: How does one use a VB... 02-27-2013, 06:09 PM
Sarvazad Re: How does one use a VB... 03-01-2013, 11:15 AM
shg Re: How does one use a VB... 03-01-2013, 11:54 AM
Sarvazad Re: How does one use a VB... 03-14-2013, 02:02 PM
  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    USA, Earth
    MS-Off Ver
    Excel 2010
    Posts
    16

    Question How does one use a VB module in an Excel sheet's code?

    Hi Everyone,

    This is my first post here, so I hope I am following the proper format. I am newer to Visual Basic, but have some back ground in C++, Java script, and Python.

    Getting to the question at hand, I am wondering just how to call a 'module' via a button in an excel sheet. Creating the button and the linked title was easy enough, but I cannot seem to get the module to be called. I know the module works on its own with no variables fed in, but every time I get the error:

    Compile error:

    Expected variable or procedure, not module
    For the code:

    Private Sub Cmd_Bttn__Find_Folder_Click()

    ' Display Message to user:
    ' ========================
    MsgBox "Please choose the folder which contains the file 'Fan_Data.xlsx'"

    ' Browse for Folder:
    ' ===================
    Call BrowseForFolder

    ' Assign folder location to a variable:
    ' -------------------------------------
    ' OpenBrowseAt = BrowseForFolder

    End Sub
    Browse for Folder Function: (in module named "BrowseForFolder") (imported as a .bas file)

    Option Explicit

    Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    ' NOTE_1: If the "OpenAt" path is provided, open the browser at that directory
    ' NOTE_2: If "OpenAt" path is invalid, function will open at the Desktop level

    Dim ShellApp As Object

    ' Create a file browser window at the default folder
    ' -----------------------------------------------------
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

    ' Set the folder to that selected. (On error in case cancelled)
    ' -----------------------------------------------------------------
    On Error Resume Next
    BrowseForFolder = ShellApp.self.path
    On Error GoTo 0

    ' Destroy the Shell Application
    ' ----------------------------------
    Set ShellApp = Nothing

    ' Check for invalid or non-entries and send to the Invalid error handler if found
    ' -------------------------------------------------------------------------------
    ' Valid selections can begin L: (where L is a letter) or
    ' \\ (as in \\servername\sharename.
    '
    ' All others are invalid
    '
    Select Case Mid(BrowseForFolder, 2, 1)

    Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid

    Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid

    Case Else
    GoTo Invalid ' (See Routine Below)

    End Select

    Exit Function

    Invalid:
    'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False

    End Function

    Do I need to copy and paste my code from the module "BrowseForFolder" into the sheet itself to make it work? If so, using modules aside from a form of back up seems entirely redundant. I'm sure there has to be a way to do this, but I am just not aware of it yet. Ideally I want to take the found folder location and assign it to the variable as shown, then use it so I can open the indicated excel file to extract data.

    Thank you in advance for any assistance!
    Last edited by Sarvazad; 03-14-2013 at 02:03 PM. Reason: Post is resolved with a solution

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1