Save csv file as an xlsx file in a different location.

Jungo

New Member
Joined
May 30, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I've see a few posts like this, but I need help with my specific set of needs:

I am struggling with syntax and language. I need a script that will run this set of logic:

-ACTIVEWORKBOOK.SAVEAS
-SAVE TO SPECIFIC DIRECTORY
-ORIGINAL FILE NAME NEEDS TO REMAIN, BUT CHANGE FROM CSV TO XLSX
-DELETE ORIGINAL
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Here is the rudimentary code to do what you've asked.

VBA Code:
Sub CopyCSVtoXLSX()
  Dim CSVFilePathName As String
  CSVFilePathName = "C:\somepath\Test1.csv"
  Dim FileNameRoot As String
  FileNameRoot = Mid$(CSVFilePathName, InStrRev(CSVFilePathName, Application.PathSeparator) + 1)
  FileNameRoot = Left$(FileNameRoot, InStr(FileNameRoot, ".") - 1)
  Dim XLSXFilePath As String
  XLSXFilePath = "C:\otherpath"
  Dim XLSXFilePathName As String
  XLSXFilePathName = XLSXFilePath & Application.PathSeparator & FileNameRoot & ".xlsx"
  Dim wb As Workbook
  Set wb = Workbooks.Open(CSVFilePathName)
  wb.SaveAs XLSXFilePathName, xlWorkbookDefault
  Kill CSVFilePathName
End Sub

You can enhance it with GetOpenFileName to select the CSV file and GetSaveAsFileName to select the output folder and filename.
 
Upvote 0
Here is the rudimentary code to do what you've asked.

VBA Code:
Sub CopyCSVtoXLSX()
  Dim CSVFilePathName As String
  CSVFilePathName = "C:\somepath\Test1.csv"
  Dim FileNameRoot As String
  FileNameRoot = Mid$(CSVFilePathName, InStrRev(CSVFilePathName, Application.PathSeparator) + 1)
  FileNameRoot = Left$(FileNameRoot, InStr(FileNameRoot, ".") - 1)
  Dim XLSXFilePath As String
  XLSXFilePath = "C:\otherpath"
  Dim XLSXFilePathName As String
  XLSXFilePathName = XLSXFilePath & Application.PathSeparator & FileNameRoot & ".xlsx"
  Dim wb As Workbook
  Set wb = Workbooks.Open(CSVFilePathName)
  wb.SaveAs XLSXFilePathName, xlWorkbookDefault
  Kill CSVFilePathName
End Sub

You can enhance it with GetOpenFileName to select the CSV file and GetSaveAsFileName to select the output folder and filename.
Hey Jon,

thanks for this. i understand most of it but what would i change CSVFilePathName = "workbook.location" to, as the location that the csv file is always changing.

activeworkbook.location?

thanks!
 
Upvote 0
If the CSV file is open and the active workbook, you could use

VBA Code:
CSVFilePathName = ActiveWorkbook.FullName

I'm sure you don't want to keep changing the code to access the CSV file and path, so you could use GetOpenFileName to get the CSV file name and path and GetSaveAsFileName to get the XLSX file name and path.

Here's a routine which does both:

VBA Code:
Sub CopyCSVtoXLSX()
  Dim sHomeDir As String
  sHomeDir = CurDir
  
  Dim vTemp As Variant
  vTemp = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", Title:="Select CSV File", MultiSelect:=False)
  If TypeName(vTemp) = "Boolean" Then
    ' False, so canceled
    GoTo ExitSub
  End If
  
  Dim CSVFilePathName As String
  CSVFilePathName = vTemp
  
  Dim FileNameRoot As String
  FileNameRoot = Mid$(CSVFilePathName, InStrRev(CSVFilePathName, Application.PathSeparator) + 1)
  FileNameRoot = Left$(FileNameRoot, InStr(FileNameRoot, ".") - 1)
  
  Dim XLSXFilePath As String
  ' if it's always the same
  XLSXFilePath = "C:\otherpath"
  ' if it is different, let's start with CSV path
  XLSXFilePath = Left$(CSVFilePathName, InStrRev(CSVFilePathName, Application.PathSeparator) - 1)
  
  Dim XLSXFilePathName As String
  XLSXFilePathName = XLSXFilePath & Application.PathSeparator & FileNameRoot & ".xlsx"
  
  vTemp = Application.GetSaveAsFilename(FileFilter:="Excel Workbooks (*.xlsx), *.xlsx", InitialFileName:=XLSXFilePathName, Title:="Select Workbook Name and Path")
  If TypeName(vTemp) = "Boolean" Then
    ' False, so canceled
    GoTo ExitSub
  End If
  
  Dim wb As Workbook
  Set wb = Workbooks.Open(CSVFilePathName)
  wb.SaveAs XLSXFilePathName, xlWorkbookDefault
  Kill CSVFilePathName
  
ExitSub:
  ChDrive sHomeDir
  ChDir sHomeDir
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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