+ Reply to Thread
Results 1 to 7 of 7

VBA to save all .CSV files in a folder as .XLSM not working?

  1. #1
    Registered User
    Join Date
    10-19-2011
    Location
    East Coast
    MS-Off Ver
    Office 365
    Posts
    33

    VBA to save all .CSV files in a folder as .XLSM not working?

    Good morning,

    I've included my VBA code below which I am running to attempt to programmatically navigate to a folder and then open all .CSV files (and also ignoring all non-.CSV files) and perform a Save As function so that the files are saved as .XLSM. I have hundreds of individual .CSV files in this folder to convert to .XLSM files (and receive additional files on a daily basis that must also be converted) and if properly executed, it will save me a ton of time and will mitigate errors caused by manually performing this operation.

    I can't seem to figure out the problem here. I've stepped through the code and I can clearly see each of my .CSV files in the targeted folder being opened and then properly closed, but the targeted files remain .CSV files instead of being changed to .XLSM files. I do not see any error messages when I run this code.

    Any assistance is greatly appreciated!

    Option Explicit
    Sub ConvertToXLSM()
    '
    ' Uses code from John Walkenbach's Power Programming book
    '
    Dim i As Long
    Dim NumFiles As Long
    Dim FileName As String
    Dim FileNames() As String

    ' Get name of first file in backlog directory
    FileName = Dir(ThisWorkbook.Path & "/*.csv")

    NumFiles = 1
    ReDim Preserve FileNames(1 To NumFiles)
    FileNames(NumFiles) = FileName

    ' Get other file names, if any
    Do While FileName <> ""
    FileName = Dir()
    If FileName <> "" Then
    NumFiles = NumFiles + 1
    ReDim Preserve FileNames(1 To NumFiles)
    FileNames(NumFiles) = FileName
    End If
    Loop

    ' Save each file as a .xlsm file, overwriting any existing .xlsm Files
    Application.DisplayAlerts = False
    For i = 1 To UBound(FileNames)
    If FileNames(i) <> ThisWorkbook.Name Then
    Workbooks.Open FileName:=ThisWorkbook.Path & "\" & FileNames(i)
    ActiveWorkbook.SaveAs FileName:=Left(FileNames(i), Len(FileNames(i)) - 4) & ".xlsm", FileFormat:=52
    ActiveWorkbook.Close
    End If
    Next i
    Application.DisplayAlerts = True

    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Hi,

    firstly, you need to edit your post to add [ CODE ] tags around the code.

    Please Login or Register  to view this content.
    Secondly, when you're stepping through the code, what happens when this line is executed:

    Please Login or Register  to view this content.
    Try commenting out the line

    Please Login or Register  to view this content.
    and check what's happening on save.

  3. #3
    Registered User
    Join Date
    10-19-2011
    Location
    East Coast
    MS-Off Ver
    Office 365
    Posts
    33

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Ahhh... great idea to un-Comment the DisplayAlerts… it says that the .XLSM file already exists and offers a Yes/No/Cancel dialogue box (screen shot attached) to choose whether or not to over-write. Out of curiosity, I chose 'Yes', and the script completed. However, the targeted files were NOT changed from .CSV to .XLSM? I looked in neighboring folders and do not see these newly-updated .XLSM files - this is confusing. It appears to be 'working', but the 'Save As' process is not completing even if I choose 'Yes'. Also, why does it think that the .XLSM file already exists when it does not? There is a .CSV file that I'm looking to open and then Save As an .XLSM file and that .XLSM vXLSMSaveAsErrorMsg_Jan19.pngersion definitely does not already exist. Thank you again!

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Don't use "ActiveWorkbook.SaveAs...", it is prone to error when you have multiple workbooks open.
    Try and set workbook object instead.

    Ex:
    Please Login or Register  to view this content.
    Also, if you don't specify the full path of file, it may be saved under default folder location (typically Documents folder).
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  5. #5
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Quote Originally Posted by patdools View Post
    However, the targeted files were NOT changed from .CSV to .XLSM? I looked in neighboring folders and do not see these newly-updated .XLSM files - this is confusing.
    Look in your standard Documents folder for the missing files.

    Sometimes "ThisWorkbook.Path" can fail or get confused. (especially if you're using it with a workbook on a network drive).

    Excel will then dump the new files it makes in the 'default' folder.

  6. #6
    Registered User
    Join Date
    10-19-2011
    Location
    East Coast
    MS-Off Ver
    Office 365
    Posts
    33

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Thank you CK76 and PaulSP8 - fantastic guidance! The documents were in fact being written to my Documents folder, so I stopped using 'ThisWorkbook.Path' and inserted the path of the target directory itself. Also, using the Workbook object instead of 'ActiveWorkbook.SaveAs' was another key change to my code that helped complete the solution. I'm even reading the .CSV files from one directory and writing the copies of the .XLSM files to another, which keeps everything cleaner for my purposes. Thank you again!

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: VBA to save all .CSV files in a folder as .XLSM not working?

    Glad we could help
    And thanks for the update.

    If your issue is resolved. Please mark the thread as solved, using thread tools found at top of your initial post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Save as xlsx from xlsm code update in the same folder as the template
    By PaulM100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2018, 05:09 AM
  2. [SOLVED] Listing only xlsm files of the folder with VBA
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2017, 10:09 AM
  3. ExecuteExcel4Macro not working now for closed .xlsm files under Windows 7
    By Edurd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2017, 05:45 PM
  4. Convert csv files in folder to xlsm
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-13-2015, 09:49 PM
  5. Replies: 2
    Last Post: 10-04-2013, 10:12 AM
  6. [SOLVED] Save As XLSM and create a new folder
    By kieranm105 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2013, 09:26 AM
  7. [closed - opening new thread] Save xlsm to Sharepoint/Web Folder
    By phi11yguy19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2011, 08:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1