Results 1 to 19 of 19

Compare files in folder with comments in sheet, rename files based on conditions

Threaded View

  1. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

    Re: Compare files in folder with comments in sheet, rename files based on conditions

    Hi sans

    Not the most elegant....
    Option Explicit
    
    Sub Rename_Files()
    Dim Fso As Object, objFolder As Object, File As Object
    Dim Path As String, Name As String, Num As String, NewName As String
    Dim cell As Range, x As Long, Word
    Application.ScreenUpdating = False
    Path = ThisWorkbook.Path & "\Txt Folder\" 'change Txt Folder to name of folder housing txt files
    Set Fso = CreateObject("Scripting.filesystemobject")
    Set objFolder = Fso.GetFolder(Path)
    For Each File In objFolder.Files
        Name = Left(File.Name, (InStrRev(File.Name, ".", -1, vbTextCompare) - 1))
        With ActiveSheet
            For Each cell In .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
                Num = Left(cell, Application.Find(" ", cell))
                Word = Split(cell, " ")
                For x = 0 To UBound(Word)
                    If Name Like "*" & Word(x) & "*" Then
                        NewName = Num & Name
                        GoTo nxt
                    End If
                Next x
            Next cell
        End With
    nxt:
        File.Name = NewName & ".txt"
    Next File
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 11-24-2018 at 06:06 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Rename .pdf files in a folder
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-11-2019, 01:11 AM
  2. Rename Sheet name for all the excel files within the folder
    By pitamine in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2017, 02:27 PM
  3. Rename Sheet name for all the excel files within the folder
    By banglong in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-12-2014, 11:45 PM
  4. [SOLVED] Rename Files in A folder
    By pradeepjassal in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-12-2012, 10:36 AM
  5. rename all the files in a folder based the names given in column A
    By aravindhan_31 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2008, 05:30 AM
  6. [SOLVED] rename files in a folder
    By Varun Nair in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-18-2006, 08:25 AM
  7. [SOLVED] rename all files in a folder
    By DIBS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2006, 02:50 AM

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