Code no longer working - Error: Variable not defined

AndyEd

Board Regular
Joined
May 13, 2020
Messages
124
Office Version
  1. 365
Platform
  1. Windows
I successfully used the below code to create a small number of folders. When triggered using a button on the spreadsheet it was working fine. Now that I have moved the code to trigger from a button (OK) within a userform, it is no longer working, displaying the error message 'Variable not defined', with the code stopping on "openAt".

I do not understand what appears to have changed.

Any help would be very much appreciated.

Thanks,

VBA Code:
Private Sub cmdOk_Click()

openAt = "My computer:"

Set ShellApp = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose where to create the folder", 0, openAt)

On Error Resume Next
BrowseForFolder = ShellApp.Self.Path

MkDir (BrowseForFolder & "\" & Workbooks("Create folders & files").Worksheets("Tracker").Range("A2").value & _
"." & Right(Workbooks("Create folders & files").Worksheets("Tracker").Range("I2"), 4) & _
" - " & Workbooks("Create folders & files").Worksheets("Tracker").Range("E2").value)

MkDir (BrowseForFolder & "\" & Workbooks("Create folders & files").Worksheets("Tracker").Range("A2").value & _
"." & Right(Workbooks("Create folders & files").Worksheets("Tracker").Range("I2"), 4) & _
" - " & Workbooks("Create folders & files").Worksheets("Tracker").Range("E2").value & "\" & "DMP")

MkDir (BrowseForFolder & "\" & Workbooks("Create folders & files").Worksheets("Tracker").Range("A2").value & _
"." & Right(Workbooks("Create folders & files").Worksheets("Tracker").Range("I2"), 4) & _
" - " & Workbooks("Create folders & files").Worksheets("Tracker").Range("E2").value & "\" & "Previous similar requests")

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Sounds like you have not declared the variable - if you use the Option Explicit - then all variables must be declared with a Dim statement or you can declare it as a Public variable.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top