VB Script Help - Automatically run script on file activatation

steve2115

Board Regular
Joined
Mar 17, 2014
Messages
82
Hi,
Need help modifying the below code.
1. I would like the script to run automatically when the file is opened.
2. Instead of browsing & selecting the file location, I need it to automatically use file location "Z:\DEPTS\SAP\SupplierRequirements"
3. When file is closed I data should be cleared from sheet
Code:
Option Explicit
 
Sub GetFileNames()
     
    Dim xRow As Long
    Dim xDirect$, xFname$, InitialFoldr$
     
    InitialFoldr$ = "C:\" '<<< Startup folder to begin searching from
    Range("C3").Select ' Where to start the list of names
    With Application.FileDialog(msoFileDialogFolderPicker) 'User input for folder to look at
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then ' If there are files in selected folder
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
                ActiveCell.Offset(xRow) = xFname$
                xRow = xRow + 1
                xFname$ = Dir
            Loop
        End If
    End With
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Unfortunately this will not work. I need to modify the code so that I do not have to browse for the file location. I need to code to run when file is opened and retrieve the file names from the specified file location.
 
Upvote 0
for #2 - getting closer...That worked. It now starts up in correct folder location however I still need to manually select it.
 
Upvote 0

Forum statistics

Threads
1,215,699
Messages
6,126,273
Members
449,308
Latest member
VerifiedBleachersAttendee

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