Modify Column format before saving as csv file

WascoWarrior

New Member
Joined
May 22, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have a button in a XLSM spreadsheet that lets the user copy the data to a new sheet and save as a CSV file.
1716386101230.png

What I need to do before saving as a csv file, is modify the format of 2 columns-dates (currently set as text with data represented as mm/dd/yy and change the column so the data is saved as mmddyy (remove the slashes) before saving as a CSV file. How can this be accomplished using VBA. I don't want the user to manually modify the fields.
Sub CmdUploadIFS_Click()
'***************************************************************************************
'*** Save Spreadsheet as csv ***
'***************************************************************************************

Dim MyPath As String
Dim MyFileName As String
Dim FullPath As String
Dim WB1 As Workbook, WB2 As Workbook
Dim rng As Range
Dim todaydate As Date

MyPath = "C:\UPLOAD\UPLOADPO"
Set WB1 = ActiveWorkbook

On Error Resume Next
Set rng = Range("A3:K9999")
If rng Is Nothing Then Exit Sub
On Error GoTo 0
Application.ScreenUpdating = False
rng.Copy

Set WB2 = Application.Workbooks.Add(1)
WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
todaydate = Now()
MyFileName = "UPLOADPO_" & Format(todaydate, "yyyymmdd_hhmmss")
FullPath = MyPath & "\" & MyFileName

Application.DisplayAlerts = False
If MsgBox("Data will be copied to: " & MyPath & "\" & MyFileName & vbCrLf & _
"Continue?", 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:=True
.Close False
End With
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Please try this.


VBA Code:
Sub CmdUploadIFS_Click()
'***************************************************************************************
'*** Save Spreadsheet as csv ***
'***************************************************************************************

  Dim MyPath As String
  Dim MyFileName As String
  Dim FullPath As String
  Dim WB1 As Workbook, WB2 As Workbook
  Dim rng As Range
  Dim todaydate As Date
  
  MyPath = "C:\UPLOAD\UPLOADPO"
  Set WB1 = ActiveWorkbook
  
  On Error Resume Next
  Set rng = Range("A3:K9999")
  If rng Is Nothing Then Exit Sub
  On Error GoTo 0
  Application.ScreenUpdating = False
  rng.Copy
  
  Application.Calculation = xlCalculationManual
  
  Set WB2 = Application.Workbooks.Add(1)
  WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
  Set Sht = ActiveSheet
  
  Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(slup)) 'Column F
  rng.NumberFormat = "@"
  
  For Each Cel In rng
    Cel.Value = Replace(Cel.Value, "/", "")
  Next Cel
  
  Set rng = Sht.Range(Range("G1"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(slup)) 'Column F
  rng.NumberFormat = "@"
  
  For Each Cel In rng
    Cel.Value = Replace(Cel.Value, "/", "")
  Next Cel
  
  
  
  todaydate = Now()
  MyFileName = "UPLOADPO_" & Format(todaydate, "yyyymmdd_hhmmss")
  FullPath = MyPath & "\" & MyFileName
  
  Application.DisplayAlerts = False
  If MsgBox("Data will be copied to: " & MyPath & "\" & MyFileName & vbCrLf & _
  "Continue?", 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:=True
    .Close False
  End With
  Application.DisplayAlerts = True
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
I tried inserting your modifications, but it is failing on the:
Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlup)).
I think (slup) was a typo so I changed it.
I'm getting Run-time error 1004 Application-defined or object-defined error.
I also moved the Application.Calculation = xlCalculationManual line after the WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues line because it was erroring out with an X400.
Any suggestions?
 
Upvote 0
Good catch on "slup". There was a second one. I fixed those in the version below.

What line is giving you the error?

The purpose for 'Application.Calculation = xlCalculationManual' is to stop Excel from recalculating. It would defeat the purpose if you moved it below the line you stated.

Make sure your table begins on row 1. If not, change these lines:
Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlUp)) 'Column F
Set rng = Sht.Range(Range("G1"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(xlUp)) 'Column F
TO:
Set rng = Sht.Range(Range("F2"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlUp)) 'Column F
Set rng = Sht.Range(Range("G2"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(xlUp)) 'Column F
or whatever row the table begins


VBA Code:
Sub CmdUploadIFS_Click()
'***************************************************************************************
'*** Save Spreadsheet as csv ***
'***************************************************************************************

  Dim MyPath As String
  Dim MyFileName As String
  Dim FullPath As String
  Dim WB1 As Workbook, WB2 As Workbook
  Dim rng As Range
  Dim todaydate As Date
  
  MyPath = "C:\UPLOAD\UPLOADPO"
  Set WB1 = ActiveWorkbook
  
  On Error Resume Next
  Set rng = Range("A3:K9999")
  If rng Is Nothing Then Exit Sub
  On Error GoTo 0
  Application.ScreenUpdating = False
  rng.Copy
  
  Application.Calculation = xlCalculationManual
  
  Set WB2 = Application.Workbooks.Add(1)
  WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
  Set Sht = ActiveSheet
  
  Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlUp)) 'Column F
  rng.NumberFormat = "@"
  
  For Each Cel In rng
    Cel.Value = Replace(Cel.Value, "/", "")
  Next Cel
  
  Set rng = Sht.Range(Range("G1"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(xlUp)) 'Column F
  rng.NumberFormat = "@"
  
  For Each Cel In rng
    Cel.Value = Replace(Cel.Value, "/", "")
  Next Cel
  
  
  
  todaydate = Now()
  MyFileName = "UPLOADPO_" & Format(todaydate, "yyyymmdd_hhmmss")
  FullPath = MyPath & "\" & MyFileName
  
  Application.DisplayAlerts = False
  If MsgBox("Data will be copied to: " & MyPath & "\" & MyFileName & vbCrLf & _
  "Continue?", 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:=True
    .Close False
  End With
  Application.DisplayAlerts = True
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
When I execute the code exactly as coded, it creates the new sheet with no data copied and I get a X400 error message. When I step thru in Debug it fails on this line now:
WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
The only new code at this point now is the:
Application.Calculation = xlCalculationManual

FYI: Yes, the data gets loaded into the new sheet starting in cell A1.
 
Upvote 0
Ok, I was able to fully test this macro and it worked


Book4 20240522.xlsm
ABCDEFGHIJKL
1
2Upload Purchase order
3CompanyPO NumberVendor NumberItemQuantityPromise Date MM/DD/YYDue Date MM/DD/YYPlannerShip ViaBuyerWarehouseError Messages
4One7894561237891105/31/20246/15/2024HimFedexHeWH12None
5Two7894601237992206/5/20246/20/2024UsUPSTheyWH30None
6Three7894641238093306/10/20246/25/2024ThemUSPSWeWH50None
Sheet1
Cell Formulas
RangeFormula
B5:B6B5=B4+4
C5:C6C5=C4+10
F5:G6F5=F4+5


VBA Code:
Sub CmdUploadIFS_Click()
'***************************************************************************************
'*** Save Spreadsheet as csv ***
'***************************************************************************************

  Dim MyPath As String
  Dim MyFileName As String
  Dim FullPath As String
  Dim WB1 As Workbook, WB2 As Workbook
  Dim rng As Range
  Dim todaydate As Date
  Dim Sht As Worksheet
  Dim Cel As Range
  Dim aStr As String
  Dim Dt As Date
  
  MyPath = "C:\UPLOAD\UPLOADPO"
  Set WB1 = ActiveWorkbook
  
  On Error Resume Next
  Set rng = Range("A3:K9999")
  If rng Is Nothing Then Exit Sub
  On Error GoTo 0
  Application.ScreenUpdating = False
  
  
  Application.Calculation = xlCalculationManual
  
  Set WB2 = Application.Workbooks.Add(1)
  rng.Copy
  WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues
  Set Sht = ActiveSheet
  
  
  Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlUp)) 'Column F
  rng.NumberFormat = "mm/dd/yyyy"
  'rng.NumberFormat = "@"
  
  For Each Cel In rng
    If IsDate(Cel) Then
      Dt = Cel.Value
      Cel.NumberFormat = "@"
      Cel.Value = Format(Dt, "mmddyy")
    End If
  Next Cel
  
  Set rng = Sht.Range(Range("G1"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(xlUp)) 'Column F
  rng.NumberFormat = "mm/dd/yyyy"
  'rng.NumberFormat = "@"
  
  For Each Cel In rng
    If IsDate(Cel) Then
      Dt = Cel.Value
      Cel.NumberFormat = "@"
      Cel.Value = Format(Dt, "mmddyy")
    End If
  Next Cel
  
  
  
  todaydate = Now()
  MyFileName = "UPLOADPO_" & Format(todaydate, "yyyymmdd_hhmmss")
  FullPath = MyPath & "\" & MyFileName
  
  Application.DisplayAlerts = False
  If MsgBox("Data will be copied to: " & MyPath & "\" & MyFileName & vbCrLf & _
  "Continue?", 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:=True
    .Close False
  End With
  Application.DisplayAlerts = True
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Still getting the X400 error
It gets this far and stops.
I'm not sure what the issue is.
I will try creating a new sheet and insert just this code and see if I get the same result.
1716411512106.png
 
Upvote 0
I feel like there may be an addin that is conflicting

Try adding Application.EnableEvents = False at the beginning of the macro and the opposing one at the end
 
Upvote 0
Ok, I was able to fully test this macro and it worked
Hello Jeff, does fully testing it mean that you ran it from a button ?
In the thread here the issue was that code was in a sheet module (as is the case when running from a button) and not a normal module.
I thought that having Range unqualified inside the brackets was a likely candidate for that issue.

@WascoWarriorthis line appears in 2 places 1 for column F and 1 for column G.
What happens if you change them both from this format:
VBA Code:
' Col F
Set rng = Sht.Range(Range("F1"), Sht.Cells(Sht.Cells.Rows.Count, 6).End(xlUp)) 'Column F
' Col G
Set rng = Sht.Range(Range("G1"), Sht.Cells(Sht.Cells.Rows.Count, 7).End(xlUp)) 'Column G

to this format:
VBA Code:
' Col F
  With Sht
    Set rng = .Range(.Range("F1"), .Cells(.Cells.Rows.Count, 6).End(xlUp)) 'Column F
  End With

' Col G
  With Sht
    Set rng = .Range(.Range("G1"), .Cells(.Cells.Rows.Count, 7).End(xlUp)) 'Column G
  End With
 
Upvote 0
Solution

Forum statistics

Threads
1,217,384
Messages
6,136,274
Members
450,001
Latest member
KWeekley08

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