Save as with original name

bradyj7

Board Regular
Joined
Mar 2, 2011
Messages
106
The code below, loops through files in a folder a processes them by calling the macro Torque_kin. Before that it checks to see if the value in cell c2 is greater than 0 and if its not it saves file with the date in cell a2 and closes.

My question is, how can I save it with it original file name? instead of renaming it with the date in cell a2.

Thanks
Code:
Sub Macro1()
    '//Change the path to the main folder, accordingly
    Call RecursiveFolders("C:\Main folder")
End Sub

Sub RecursiveFolders(ByVal MyPath As String)

    Dim FileSys As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim objFile As Object
    Dim wkbOpen As Workbook
    
    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set objFolder = FileSys.GetFolder(MyPath)

    Application.ScreenUpdating = False
    
    For Each objSubFolder In objFolder.SubFolders
    
        For Each objFile In objSubFolder.Files
            Set wkbOpen = Workbooks.Open(FileName:=objFile)
            
            'Check first speed value, should be = 0, if not save file to 'bad data' folder and close
             Range("c2").Select
             If ActiveCell.Value > 0 Then
            Application.DisplayAlerts = False
              ActiveSheet.Copy
              ActiveWorkbook.SaveAs FileName:="C:\Bad data\" & Format(ActiveSheet.Range("A2"), "YYYYMMDDHHMMSS") & ".xls"
             ActiveWorkbook.Close
    
    Else
     'Call macro here
            Call torque_kin
    End If
           
            'Change to true if you want to save raw and kinematic sheets etc
        wkbOpen.Close savechanges:=False
        Next
        Call RecursiveFolders(objSubFolder.Path)
    Next

    Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Well the files are in one folder and I want to save the "bad files" to another folder.
Do you mean something like this?
Code:
'Check first speed value, should be = 0, if not save file to 'bad data' folder and close
             Range("c2").Select
             If ActiveCell.Value > 0 Then
             Application.DisplayAlerts = False
             ActiveWorkbook.Save FileName:="C:\Bad data\.xls"
             ActiveWorkbook.Close
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top