VB to select path from cell

gaz_chops

Well-known Member
Joined
Apr 29, 2003
Messages
6,485
Platform
  1. MacOS
I have the following macro I recorded, I need to amend it so that the 'Path' is populated from a cell which the user can enter -

VBA Code:
Sub XeroCSV()
'
' XeroCSV Macro
'

'
    Dim FileName As String
    Dim Path As String
    Path = "\\?????????\home\????????\?????????\Documents\Xero Reports\Payroll Data for Xero\Xero CSV Imports\"
    FileName = Range("G2").Value & ".csv"
    Sheets("Xero Import").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
    'ActiveWorkbook.SaveAs Path & FileName, xlCSV
    Application.DisplayAlerts = True
    ActiveWindow.Close
    Sheets("Journal").Select
    Range("G5").Select
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Let's say the cell in "A2".
Then just change this line:
VBA Code:
Path = "\\?????????\home\????????\?????????\Documents\Xero Reports\Payroll Data for Xero\Xero CSV Imports\"
to this:
VBA Code:
MyPath = Range("A2")
(Note: You should NOT use reserved words like "Path" as the name of variables! It can cause errors and other unexpected behavior!)

Make sure that the last "\" is at the end of that path.
I will sometimes do the following, in case people forget to do that.
VBA Code:
MyPath = Range("A2")
If Right(MyPath,1)<>"\" Then MyPath = MyPath & "\"
 
Upvote 0
Thanks Joe, for some reason it is saving it into the original files 'Path' not the one noted in the cell!
 
Upvote 0
Thanks Joe, for some reason it is saving it into the original files 'Path' not the one noted in the cell!
You need to change this line here too:
Rich (BB code):
    ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
like this:
Rich (BB code):
    ActiveWorkbook.SaveAs FileName:=MyPath & FileName, FileFormat:=xlCSV, Local:=True
to use the new name we picked for the variable.
Otherwise, it will use the reserved value ("Path" is the path of the current file)!

This is a good example why not to use reserved words as variables - your code can produce enexpected/unintended consequences!
 
Upvote 0
Solution
Think I found it, do i need to change this bit as well - Path to MyPath

ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
 
Upvote 0

Forum statistics

Threads
1,214,873
Messages
6,122,029
Members
449,061
Latest member
TheRealJoaquin

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