Need to rename to required file name if given cell value matches

praveenreddy

New Member
Joined
Mar 8, 2019
Messages
1
Hi,, i am looping all the files present in a folder and searching for the given cell value "Order Date". If this cell value matches in cell range C2, then rename the excel file name as "Praveen.xls".

My code is below and after this line (If ActiveSheet.Range("C2").Value = "Order Date" Then) it is directly going to end the if statement. File is not saving with required file name. Can any one can help on this..

Sub Rename()

Dim myfile As String
Dim oldfname As String, newfname As String
Dim ActualFileCSV
Dim folderName As String
Dim workfileXLSX

Application.DisplayAlerts = False
Application.ScreenUpdating = False

' Capture name of current file
myfile = ActiveWorkbook.Name

' Set folder name to work through
folderName = "%my file path%"

Application.DisplayAlerts = False
Application.ScreenUpdating = False

' Delete Column in the excel
workfileXLSX = Dir(folderName & "*.xls")
Do While workfileXLSX <> ""
' Open xlsx file
Workbooks.Open Filename:=folderName & workfileXLSX



If ActiveSheet.Range("C2").Value = "Order Date" Then
Workbook.SaveAs Filename:=folderName & workfileXLSX, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
' ActiveWorkbook.Save
ActiveWorkbook.Close
End If
workfileXLSX = Dir()
Loop

End Sub
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Watch MrExcel Video

Forum statistics

Threads
1,109,426
Messages
5,528,690
Members
409,830
Latest member
KT50

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top