Cannot run VBA code in Excel 365

johnbrownbaby

New Member
Joined
Dec 9, 2015
Messages
38
Hello,

I have this VBA code written in the "ThisWorkbook" section of my excel file to list the filenames from a folder:


VBA Code:
On Error Resume Next

Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H200
Const BIF_RETURNONLYFSDIRS = &H1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders("MyDocuments")
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)

Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\filelist.txt", True)
Set objFolder = objFSO.GetFolder(strFolderPath)
' CHANGE STARTS HERE :
Set objColFolders = objFolder.Folders

For Each tfolder In objColFolders
    objNewFile.WriteLine (tfolder.Name)
Next
' CHANGE DONE.
objNewFile.Close

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem

    On Error Resume Next

    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
    If (objFolder Is Nothing) Then
        Wscript.Quit
    End If
    Set objFolderItem = objFolder.Self
    Browse4Folder = objFolderItem.Path
    Set objFolderItem = Nothing
    Set objFolder = Nothing
End Function

I am not able to run the code in excel as the run button does not do anything. It used to work before, however now I am not able to execute the code.

I went into the Trust Center and "Enable VBA macros" but I still cannot execute the code. Please tell me what I am doing wrong.

Thanks!

Please tell me what I am doing wrong.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
The bottom part is a Function not a Sub and so you would need a sub to call the function, do you have the top part within a Sub because it doesn't look like it?
If you do then please post your full code (if you have it in something like a Workbook_Open code then you wouldn't "Run" it as that is for regular Subs).
 
Upvote 0
Hello Mark858, Thanks for your response.

That is all I have in the code window. I was modifying the code so probably I inadvertently deleted some code. Can you tell me what I may need to get the code functional again?

Thanks!
 
Upvote 0
You need something like
VBA Code:
Sub YourSubname()
'The top part of your code
End Sub

and then both the Sub and the Function go in a regular module and then you run YourSubname
 
Upvote 0
@johnbrownbaby , you've obtained a portion of VB script code, which is supposed to be executed by a CSript or WScript run-time. Therefore the code has to be amended to be suitable running by VBA. Also note that the code in its current form will not work because the Folder object does not have a property called Folders. No doubt the SubFolders property is meant, but VB script bypasses that, gives no screen message and you wonder why nothing happened.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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