+ Reply to Thread
Results 1 to 3 of 3

VBA code-file name replace dash with space

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    59

    Post VBA code-file name replace dash with space

    Hello,

    I currently have this working macro in excel 2007 that will find any file in a folder of the format ???-???-???-??? ???-???-???.jpg and replace the space resulting in a changed file name called ???-???-???-???-???-???-???.jpg

    I wanted to know if there was a way to reverse the procedure. If i could somehow given a file name of ???-???-???-???-???-???-???.jpg change it to look like ???-???-???-??? ???-???-???.jpg.

    I already tried switching these statements in the code and it did not work for me.

    The current working code is listed below


    Sub RenameFiles()
    shell "cmd.exe /c ren ""C:\Documents and Settings\Desktop\pictues\???-???-???-??? ???-???-???.jpg"" ???-???-???-???-???-???-???.jpg", vbNormalFocus
    End Sub
    Thanks
    Last edited by vargs; 06-28-2010 at 03:26 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code-file name replace dash with space

    Here's how I would do that:
    Option Explicit
    
    Sub RenameFiles()
    Dim fName
    Dim fPath:  fPath = "C:\Documents and Settings\Desktop\pictures\"
    
    fName = Dir(fPath & "???-???-???-???-???-???-???.jpg")
    
    Do While Len(fName) > 0
        Name fPath & fName As _
            fPath & Application.WorksheetFunction.Substitute(fName, "-", " ", 4)
        fName = Dir
    Loop
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-18-2010
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: VBA code-file name replace dash with space

    thanks for the reply, It worked just fine
    I appreciate it

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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