sanantonio
Board Regular
- Joined
- Oct 26, 2021
- Messages
- 124
- Office Version
- 365
- Platform
- Windows
VBA Code:
Sub save()
Dim fName As String
Dim path As String
Dim defaultPath As String
defaultPath = "T:\"
path = Sheet1.Range("D18").Value
If path = "" Then
path = defaultPath
End If
If Right(path, 1) <> "\" Then
path = path & "\"
End If
fName = Range("A1")
Application.DisplayAlerts = False
On Error Resume Next
Sheets("CSV").Visible = True
Sheets("CSV").Select
Range("B34").Select
Sheets("Order Upload DIS").Select
Range("Table3[Copy all lines as of A3]").Select
Selection.Copy
Sheets("CSV").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim theNewWorkbook As Workbook
Dim currentWorkbook As Workbook
Application.EnableEvents = False
'currentWorkbook is the source workbook, create a new workbook referencing to it with theNewWorkbook
Set currentWorkbook = ActiveWorkbook
Set theNewWorkbook = Workbooks.Add
'do the copy (it's better to check if there is already a 'Worksheet 1' in the new workbook. It it exists delete it or rename it
currentWorkbook.Worksheets("CSV").Copy before:=theNewWorkbook.Sheets(1)
'Remove default sheets in order to have only the copied sheet inside the new workbook
Dim i As Integer
For i = theNewWorkbook.Sheets.Count To 2 Step -1
theNewWorkbook.Sheets(i).Delete
Next i
'Save File as XLSM
saveLocation = defaultPath & fName & ".csv"
theNewWorkbook.SaveAs Filename:=saveLocation, FileFormat:=xlCSV
theNewWorkbook.Close
Sheets("CSV").Visible = False
Sheets("Control").Select
MsgBox saveLocation
End Sub
Basically it should only be saving to the defaultPath is D18 is blank. But regardless of whether D18 is blank or not it always saves to the default. Anyone see where I'm going wrong?
D18 contains: K:\New Folder\Testing\