Use VBA to Specify New Source Filename and Location

DrDave6

New Member
Joined
Oct 1, 2014
Messages
20
I have a folder with around 40 spreadsheets, and each has a OLEDB Connection. I've written a macro that process each spreadsheet in the folder, and that works! :)

I'm need to change the source filename, and location, using VBA.

Assume the following:

- Connection Name is "ThePrices"
- new filename is "NewPrices.xlsx"
- new filelocation is "c:\Users\David\Documents\MySourceFiles"

All help is appreciated....
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This will allow the user to select a file

Code:
Option Explicit




Sub openfile()
    Dim sFil   As String
    Dim sTitle As String
    Dim sWb    As String
    Dim iFilterIndex As Integer
on error goto err_handler
    ' Set up list of file filters
    sFil = "Excel Files (*.xls),*.xls"
    ' Display *.xls by default
    iFilterIndex = 1
    ' Set the dialog box caption
    sTitle = "Select  File to Zip"
    ' Get the filename
    sWb = Application.GetOpenFilename(sFil, iFilterIndex, sTitle)


    Workbooks.Open Filename:=sWb
Exit Sub
err_handler:
Msgbox "No selection made"
End Sub
 
Upvote 0
This will allow the user to select a file

Code:
Option Explicit




Sub openfile()
    Dim sFil   As String
    Dim sTitle As String
    Dim sWb    As String
    Dim iFilterIndex As Integer
on error goto err_handler
    ' Set up list of file filters
    sFil = "Excel Files (*.xls),*.xls"
    ' Display *.xls by default
    iFilterIndex = 1
    ' Set the dialog box caption
    sTitle = "Select  File to Zip"
    ' Get the filename
    sWb = Application.GetOpenFilename(sFil, iFilterIndex, sTitle)


    Workbooks.Open Filename:=sWb
Exit Sub
err_handler:
Msgbox "No selection made"
End Sub

Did you mean to post this for another thread. I'm looking for the VBA code that I can use to change the source filename and location. There is no user interaction.
 
Upvote 0
How else would you change the file?

Without seeing the code it's not clear at all what you mean.
 
Upvote 0
Sorry for the confusion royUK...

There is no code, yet. Imagine that i have a workbook open, and, using VB, I want to change the name and location of an external source file, that I'm currently using in an OLEDB connection. I provided the details in my original message that could be used in the example.
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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