VBA Excel file to CSV, keeps CSV filename same as original workbook

前端 未结 3 1853
鱼传尺愫
鱼传尺愫 2021-01-27 15:36

I am trying to find a fast way to save my xlsx files as csv files with the same file-name as the xlsx file (just in csv forma

相关标签:
3条回答
  • 2021-01-27 15:57

    Right now, you've got the file-name hard-coded in after "ActiveWorkbook.SaveAs" so it's saving everything with that hard-coded name.

    I think you'll want to use "ActiveWorkbook.Name" to get the name of the current file and concatenate it into the "Filename" variable that you have there (without the file extension) with the new extension. For example:

    "C:\Users\padd\Desktop\NEW CSV...ok!\" & Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1) & ".csv")
    

    This is a kind of a dirty way to do it, but it should serve your needs. Also, depending on which version of Excel you use, I think you might need to use "ThisWorkbook" instead of "ActiveWorkbook" but I'm not sure.

    0 讨论(0)
  • 2021-01-27 16:02

    Before saving as csv, get the name of the xls file. You can use the ActiveWorkbook.Nameproperty. Assuming that file is called something.xls(and not .xlsx), try this:

    Sub Macro1()
        XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
        ActiveWorkbook.SaveAs Filename:="C:\Users\Username\Desktop\" & XLSName & ".csv", _
            FileFormat:=xlCSV, CreateBackup:=False
    End Sub
    

    This pulls the workbook name, cuts off the last 4 characters (".xls") and then runs the Save As command appending ".csv" to that. In case your Excel file has the xlsx extension, change line 2 to:

    XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
    

    Let me know if this works for you.

    0 讨论(0)
  • 2021-01-27 16:18

    I would organize the pieces before stitching them together with standard string concatenation. Here is the relevant section of the code using the InStr function.

    Dim myPath As String, myFileName As String
    
    myPath = "C:\Users\paddy\Desktop\NEW CSV files whole CGM date ok!"
    'possible alternate that gets the environment variable USERNAME
    'myPath = "C:\Users\" & Environ("USERNAME") & "\Desktop\NEW CSV files whole CGM date ok!"
    
    'check if the folder exists and if not create it
    If Not CBool(Len(Dir(myPath, vbDirectory))) Then _
        MkDir Path:=myPath
    
    
    myFileName = Left(ActiveWorkbook.Name, InStr(1, ActiveWorkbook.Name, ".xl", vbTextCompare) - 1)
    'you don't actually need .csv as the extension if you are explicitly saving as xlCSV or xlCSVMac but here is an alternate
    'myFileName = Left(ActiveWorkbook.Name, InStr(1, ActiveWorkbook.Name, ".xl", vbTextCompare) - 1) & ".csv"
    
    'output to the VBE's Immediate window for checking later in case there is a problem
    Debug.Print myPath & Chr(92) & myFileName
    
    ' the backslash is ASCII character 92
    ActiveWorkbook.SaveAs Filename:=myPath & Chr(92) & myFileName, _
        FileFormat:=xlCSVMac, CreateBackup:=False
    

    I'm not sure what all the scrolling was doing; it probably isn't necessary. You might want to add in the number formatting command.

    0 讨论(0)
提交回复
热议问题