Changing path location via dropdown

billy1989

New Member
Joined
Mar 22, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi all,

The below code works great, but I was wondering how to tweak it slightly - I have the path set, but was looking to see if I could set a dropdown for months, and have it set the path as what it currently is, but point to a folder within there corresponding to the month?

eg path is "D:\Carcharias Creative\CSVs", I'd like to select a dropdown when naming the file for months, and have it save in "D:\Carcharias Creative\CSVs\August" for example

VBA Code:
Sub ExportAsCSV()

Dim MyFileName As String
Dim Item As String
Dim Path As String
Dim CurrentWB As Workbook, TempWB As Workbook
Dim myrangeNA As Range
Dim myRangeCSV As Range
Path = "D:\Carcharias Creative\CSVs"

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.Worksheets("CSV").Activate
Set myrangeNA = Application.InputBox(prompt:="Select a range to copy (include header row).", Type:=8)
Item = InputBox("Please input filename", "Filename")

Set TempWB = Application.Workbooks.Add(1)
myrangeNA.Copy Destination:=TempWB.Worksheets("Sheet1").Range("A1")

MyFileName = Path & "\" & Item & ".csv"

Application.DisplayAlerts = True
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSVUTF8, CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I'd like to select a dropdown when naming the file for months, and have it save in "D:\Carcharias Creative\CSVs\August" for example

You could use a Data Validation In-cell dropdown with a list of the month names:

1688158058560.png


and read the cell's value in your code.

For example, if the DV cell is cell B2 on Sheet2, add this to your code:
VBA Code:
    Path = Path & "\" & Worksheets("Sheet2").Range("B2").Value
    If Dir(Path, vbDirectory) = vbNullString Then MkDir Path
 
Upvote 1
You could use a Data Validation In-cell dropdown with a list of the month names:

View attachment 94547

and read the cell's value in your code.

For example, if the DV cell is cell B2 on Sheet2, add this to your code:
VBA Code:
    Path = Path & "\" & Worksheets("Sheet2").Range("B2").Value
    If Dir(Path, vbDirectory) = vbNullString Then MkDir Path
Perfect, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,532
Messages
6,172,875
Members
452,486
Latest member
standw01

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