tzcoding
New Member
- Joined
- Mar 17, 2023
- Messages
- 8
- Office Version
- 2016
- Platform
- Windows
Situation:
Here is what i have working now. The VBA code will save a copy of the current sheet as an archive file in .CSV format. I'm able to assigned it to a button and it will do it on a sheet by sheet basses. This means what ever sheet the button is assigned to it will export that sheet and label it accordingly.
Help Needed:
I need help altering the code so that i can also select the file path or provide it a file path directly so i don't have to do it manually.
Here is what i have working now. The VBA code will save a copy of the current sheet as an archive file in .CSV format. I'm able to assigned it to a button and it will do it on a sheet by sheet basses. This means what ever sheet the button is assigned to it will export that sheet and label it accordingly.
Help Needed:
I need help altering the code so that i can also select the file path or provide it a file path directly so i don't have to do it manually.
VBA Code:
Sub Export_CSV()
'***************************************************************************************
'purpose: export current spreadsheet to csv.file to the same file path as source file
'
' !!NOTE!! files with same name and path will be overwritten
'***************************************************************************************
Dim MyPath As String
Dim MyFileName As String
Dim WB1 As Workbook, WB2 As Workbook
Set WB1 = ActiveWorkbook
'(1) either used range in active sheet..
'ActiveWorkbook.ActiveSheet.UsedRange.Copy
'(2) or alternatively, user selected input range:
Dim rng As Range
Set rng = Application.InputBox("select cell range with changes", "Cells to be copied", Default:="Select Cell Range", Type:=8)
Application.ScreenUpdating = False
rng.Copy
Set WB2 = Application.Workbooks.Add(1)
WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
MyFileName = "CSV_Export_" & Format(Date, "ddmmyyyy")
FullPath = WB1.Path & "\" & MyFileName
Application.DisplayAlerts = False
If MsgBox("Data copied to " & WB1.Path & "\" & MyFileName & vbCrLf & _
"Warning: Files in directory with same name will be overwritten!!", vbQuestion + vbYesNo) <> vbYes Then
Exit Sub
End If
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
With WB2
.SaveAs Filename:=FullPath, FileFormat:=xlCSV, CreateBackup:=False
.Close False
End With
Application.DisplayAlerts = True
End Sub