+ Reply to Thread
Results 1 to 8 of 8

vba renaming a file and changing file type

  1. #1
    Registered User
    Join Date
    09-10-2012
    Location
    texas usa
    MS-Off Ver
    Excel 2010
    Posts
    70

    vba renaming a file and changing file type

    I have 4 files in a folder and want to use VBA to rename and change file type
    CSV to XLSX

    Please see attached excel workbook

    Thanks Carroll
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: vba renaming a file and changing file type

    See next code
    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    09-10-2012
    Location
    texas usa
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: vba renaming a file and changing file type

    Thanks PCI
    I made some changes
    CURRENTfolder = "c:\# CURRENT RIBBSTEST AUTOMATING\"
    commented out XlsFolder = "C:\Users\user\Downloads\JOB7\" I want to stay in "c:\# CURRENT RIBBSTEST AUTOMATING\" and then kill all "csv" files, leaving only the xlsx files
    My changes don't work - stops in the first if statement - don't understand why?
    Thanks for your help


    Option Explicit

    Sub Macro1()

    Dim CURRENTfolder As String
    Dim XlsFolder As String
    Dim fname As String
    Dim wBook As Workbook

    Dim COUNTER As Long
    COUNTER = 1

    CURRENTfolder = "c:\# CURRENT RIBBSTEST AUTOMATING\"
    ' XlsFolder = "C:\# CURRENT RIBBSTEST AUTOMATING\"


    fname = Dir(CURRENTfolder & "*.csv")

    Do While fname <> ""
    Set wBook = Workbooks.Open(CURRENTfolder & fname, Format:=6, DELIMITER:=",")

    If COUNTER = 1 Then _
    ActiveWorkbook.SaveAs Filename:=CURRENTfolder & "DALLAS.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    If COUNTER = 2 Then _
    ActiveWorkbook.SaveAs Filename:=CURRENTfolder & "FRISCO.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook

    If COUNTER = 3 Then _
    ActiveWorkbook.SaveAs Filename:=CURRENTfolder & "MCKINNEY.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook

    If COUNTER = 4 Then _
    ActiveWorkbook.SaveAs Filename:=CURRENTfolder & "RICHARDSON.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook

    wBook.Close False
    fname = Dir
    COUNTER = COUNTER + 1
    Loop
    Kill CURRENTfolder & "*.csv"


    End Sub

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: vba renaming a file and changing file type

    You forgot the code tags : Just select the code and clic on the # icon
    Where the code stops?
    Next statement
    Please Login or Register  to view this content.
    must be
    Please Login or Register  to view this content.
    no ' at the beginning

  5. #5
    Registered User
    Join Date
    09-10-2012
    Location
    texas usa
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: vba renaming a file and changing file type

    PCI thanks for your patience

    These lines in your code
    CURRENTfolder = "C:\Users\user\Downloads\JOB7\"
    XlsFolder = "C:\Users\user\Downloads\JOB7\"
    the files I want to rename are in folder "C:\# CURRENT RIBBSTEST AUTOMATING\"
    so shouldn't my code be
    CURRENTfolder = "C:\# CURRENT RIBBSTEST AUTOMATING\"
    XlsFolder = "C:\# CURRENT RIBBSTEST AUTOMATING\"

    Also I want to put the code in commandbutton 3 not a macro
    can I just copy your code - starting with the line Dim CURRENTfolder As String
    carroll

  6. #6
    Registered User
    Join Date
    09-10-2012
    Location
    texas usa
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: vba renaming a file and changing file type

    Also, I don't understand your instructions...
    You forgot the code tags : Just select the code and clic on the # icon
    Where the code stops?
    Next statement

    When I select your code I don't see a # icon where the code stops
    carroll

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: vba renaming a file and changing file type

    so shouldn't my code be
    Please Login or Register  to view this content.
    Yes and it was not like that there was a ===> ' at the beginning of the last one
    can I just copy your code - starting with the line Dim CURRENTfolder As String
    YES


    Also, I don't understand your instructions...
    You forgot the code tags : Just select the code and clic on the # icon
    Where the code stops?
    Next statement

    When I select your code I don't see a # icon where the code stops

    Just above the window where you write you answer or thread there is a ribbon with 17 icons
    See file attached
    Attached Files Attached Files
    Last edited by PCI; 12-28-2019 at 07:00 PM.

  8. #8
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,307

    Re: vba renaming a file and changing file type

    Possibly...
    Please Login or Register  to view this content.
    Last edited by dangelor; 12-28-2019 at 07:35 PM. Reason: previous sub failed

+ 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] After changing file location or renaming,custom ribbon not working
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2016, 08:31 PM
  2. Changing the file type of exported document
    By jj4jj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2015, 08:02 AM
  3. Save as macro that specifies file type, file location and takes file name from three cells
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-13-2013, 10:09 PM
  4. #Value! When changing file type from XLSX to XLS
    By Khaldon in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-13-2013, 07:55 PM
  5. Renaming the file name (or part of the file name) from a list
    By bbottjen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-02-2012, 08:30 PM
  6. Replies: 15
    Last Post: 08-27-2012, 10:13 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