Hi @Nick B,
Thank you for posting your question in the Microsoft Q&A forum.
First, as I understand it, your need is to modify the save path logic within your Excel macro. This would prevent it from prompting the user to select a folder every time it runs, and instead, automatically save to a specific, pre-determined path. Is that correct?
Could you please tell me which version of Excel you're using? You can find this by going to: Excel > File > Account > About Excel.
Based on the documentation you provided and common scenarios, I've found a few options that might work for you. I'm sending them for your review.
Currently, your macro uses a file dialog box for the user to select the save folder:
VBA
With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select folder to save CSV files" If .Show <> -1 Then Exit Sub savePath = .SelectedItems(1) & "\" End With
Then, it constructs the lastFileName using this savePath:
VBA
lastFileName = savePath & baseFileName & " (" & partNum & ")" & ".csv"
If you want to change this save path programmatically instead of asking the user every time, here are your options:
Option 1: Hardcode the Save Path
If the save path will always be the same, you can simply define it directly in your code.
VBA
Sub SplitAndExportCSV2() ' ... (your existing Dim statements) ... ' Settings chunkSize = 1000 startRow = 2 partNum = 1 ' --- MODIFICATION START --- ' Define your desired save path directly Dim fixedSavePath As String fixedSavePath = "C:\Your\Desired\Folder" ' <--- Change this to your preferred path! savePath = fixedSavePath ' --- MODIFICATION END --- Set ws = ThisWorkbook.Sheets(1) lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Remove or comment out the FileDialog block if you hardcode the path ' With Application.FileDialog(msoFileDialogFolderPicker) ' .Title = "Select folder to save CSV files" ' If .Show <> -1 Then Exit Sub ' savePath = .SelectedItems(1) & "" ' End With Application.ScreenUpdating = False ' ... (rest of your code) ... End Sub
Note: Make sure the folder specified in fixedSavePath actually exists, or your macro will throw an error when trying to save the files.
Option 2: Get the Save Path from a Cell in Excel
You could store the desired save path in a specific cell on one of your worksheets. This allows for easier changes without editing the VBA code directly.
For example, if you put the save path in cell A1 of a sheet named "Settings":
VBA
Sub SplitAndExportCSV2() ' ... (your existing Dim statements) ... ' Settings chunkSize = 1000 startRow = 2 partNum = 1 ' --- MODIFICATION START --- ' Get save path from a cell (e.g., Sheet "Settings", Cell A1) On Error GoTo ErrorHandler savePath = ThisWorkbook.Sheets("Settings").Range("A1").Value If Right(savePath, 1) <> "" Then savePath = savePath & "" ' Ensure trailing backslash ' --- MODIFICATION END --- Set ws = ThisWorkbook.Sheets(1) lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Remove or comment out the FileDialog block ' With Application.FileDialog(msoFileDialogFolderPicker) ' .Title = "Select folder to save CSV files" ' If .Show <> -1 Then Exit Sub ' savePath = .SelectedItems(1) & "" ' End With Application.ScreenUpdating = False ' ... (rest of your code) ... Exit Sub ErrorHandler: MsgBox "Error: Could not retrieve save path from 'Settings' sheet or path is invalid. Please ensure Sheet 'Settings' exists and cell A1 contains a valid folder path.", vbCritical Application.ScreenUpdating = True End Sub
Notes:
- You'll need a sheet named "Settings" (or whatever you choose) and a valid path in cell A1.
- I've included basic error handling for cases where the sheet or cell isn't found, or if the path is missing a trailing backslash.
Option 3: Use the Workbook's Current Path
If you want the CSVs to be saved in the same folder where your Excel macro workbook is located, you can use ThisWorkbook.Path.
VBA
Sub SplitAndExportCSV2() ' ... (your existing Dim statements) ... ' Settings chunkSize = 1000 startRow = 2 partNum = 1 ' --- MODIFICATION START --- ' Set savePath to the current workbook's directory savePath = ThisWorkbook.Path & "" ' --- MODIFICATION END --- Set ws = ThisWorkbook.Sheets(1) lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Remove or comment out the FileDialog block ' With Application.FileDialog(msoFileDialogFolderPicker) ' .Title = "Select folder to save CSV files" ' If .Show <> -1 Then Exit Sub ' savePath = .SelectedItems(1) & "" ' End With Application.ScreenUpdating = False ' ... (rest of your code) ... End Sub
Please review these options and let me know which suits how you'd like to manage the save path for your macro. Once you've chosen your preferred method, remember to remove or comment out the Application.FileDialog block so it no longer prompts the user for a folder selection.
If there's anything else at all I can assist with, or if you have any further questions, please don't hesitate to reach out. I'm always here to listen and support you.
Looking forward to your reply!
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.