Hi everyone,
I have been struggling to get a macro working all day and wanted to see if anyone had any advice. Basically, I have a macro that copies the active sheet and saves it as a .txt file. Right now, when the Save As prompt opens, it autofills the file name with a value from a specific cell and the default save location is C:\Users\User\Documents. However, I would like to change the default save location for this file to be the file directory where the original Excel file is located. The problem I'm having is that the file will be stored on a network location (\\Folder1\Folder2\ExcelFile.xlsm). I was able to get the macro to work on a local drive using ChDrive/ChDir, but this doesn't work on a network location. Does anyone know how to do this? My code is below (I left in the code I was using for the local drives that doesn't work when the file is stored on the network drive).
Essentially I want the Save As prompt to open in \\Folder1\Folder2\ (an unmapped network location).
Thanks in advance!
I have been struggling to get a macro working all day and wanted to see if anyone had any advice. Basically, I have a macro that copies the active sheet and saves it as a .txt file. Right now, when the Save As prompt opens, it autofills the file name with a value from a specific cell and the default save location is C:\Users\User\Documents. However, I would like to change the default save location for this file to be the file directory where the original Excel file is located. The problem I'm having is that the file will be stored on a network location (\\Folder1\Folder2\ExcelFile.xlsm). I was able to get the macro to work on a local drive using ChDrive/ChDir, but this doesn't work on a network location. Does anyone know how to do this? My code is below (I left in the code I was using for the local drives that doesn't work when the file is stored on the network drive).
Essentially I want the Save As prompt to open in \\Folder1\Folder2\ (an unmapped network location).
Code:
Sub SaveToTxt()
Dim xRet As Long
Dim xFileName As Variant
On Error GoTo ErrHandler:
ChDrive Left(ActiveWorkbook.Path, 2) 'only works on local drives
ChDir ActiveWorkbook.Path 'only works on local drives
xFileName = Application.GetSaveAsFilename(ActiveWorkbook.Sheets("SheetName").Range("B3"), "Text File (*.txt), *.txt", , "Save your file as a Tab Delimited TXT")
If xFileName = False Then Exit Sub
If Dir(xFileName) <> "" Then
xRet = MsgBox("File '" & xFileName & "' exists. Overwrite?", vbYesNo + vbExclamation, "Overwrite File?")
If xRet <> vbYes Then
Exit Sub
Else
Kill xFileName
End If
End If
ActiveSheet.Copy
ActiveWorkbook.SaveAs xFileName, xlText
If ActiveWorkbook.Name <> ThisWorkbook.Name Then
ActiveWorkbook.Close False
End If
My_Exit:
Exit Sub
ErrHandler:
MsgBox Err.Description, , "Error"
End Sub
Thanks in advance!