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:
For the code:Compile error:
Expected variable or procedure, not module
Browse for Folder Function: (in module named "BrowseForFolder") (imported as a .bas file)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
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!
Bookmarks