+ Reply to Thread
Results 1 to 5 of 5

Macro to change file types

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Macro to change file types

    Is there a macro that can change all the files in a location to .csv files.
    I am working on a macro that lets me open a file at a specific location it works, but I have to change the file type to .csv 1st. I was hoping the macro could do it for me.
    Here is some of the code and the location of the need to be .csv files:

    TheFile = "\\datawhse\root\LAW81\LAWSON\print\EHARSKEY"
        CreateObject("WScript.Shell").CurrentDirectory = TheFile
        
        TheFile = Application.GetOpenFilename("Excel Files (*.*), *.*", , "Select the file and choose open.")
        If TheFile = "False" Then
            Exit Sub
        End If
        Workbooks.Open Filename:=TheFile
    So, I need code that will go to that path and change all files to .csv files before I go there with this string of code to open the desired files.
    Thank you very much for your time and help.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Erock24,

    I revised your macro to include the code needed to rename the file the user selects as a CSV file.
    Sub ChangeFileToCSV()
      Dim BS As Long, Dot As Long
      Dim Oldfilename As String
      Dim Oldfilepath As String
      Dim Newfilename As String
      
    
        TheFile = "\\datawhse\root\LAW81\LAWSON\print\EHARSKEY"
        CreateObject("WScript.Shell").CurrentDirectory = TheFile
        
        TheFile = Application.GetOpenFilename("Excel Files (*.*), *.*", , "Select the file and choose open.")
          If TheFile = "False" Then Exit Sub
    
          Oldfilename = TheFile
    
        ' Separate the file path and file name
          BS = InStrRev(Oldfilename, "\")
          Oldfilepath = Left(Oldfilename, BS)
          Newfilename = Right(Oldfilename, Len(Oldfilename) - BS)
        
        ' Add the CSV extension to the file name
          Dot = InStr(1, Newfilename, ".")
          Newfilename = Oldfilepath & Left(Newfilename, Dot) & "csv"
        
        ' Rename the file
          Name Oldfilename As Newfilename
    
        Workbooks.Open Filename:=TheFile
    End Sub
    Sincerely,
    Leith Ross

  3. #3
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299
    I'm getting a large error message saying the file can't be found. It's run time 1004

    It highlights in yellow this:

    Workbooks.Open Filename:=TheFile

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Erock24,

    Forgot to change that the last line. Should be...
    Workbooks.Open Filename:=Newfilename
    Sincerely,
    Leith Ross

  5. #5
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299
    Wow. that Works great thank you very much. awesome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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