VBA save as

MRxlicius

New Member
Joined
Apr 5, 2018
Messages
21
In the following code, i am trying to adjust so i loop through all the files on a tab, and if the range has formulas to convert it to value. But only for the specify tab. Before closing and saving the file, i want to save it as the current name and add in the end " _v2". Can someone help on that?



Sub LoopAllExcelFilesInFold2()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
DoEvents
On Error Resume Next

Sheets("Cubes Act'20").Select
For Each rng In ActiveSheet.UsedRange
If rng.HasFormula Then
rng.Formula = rng.Value
End If
Next rng








'Save and Close Workbook
'wb.Close SaveChanges:=True
wb.Close ActiveWorkbook.SaveCopyAs("filename" & "_WL")

'Ensure Workbook has closed before moving on to next line of code
DoEvents

'Get next file name
myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

rlv01

Well-known Member
Joined
May 16, 2017
Messages
771
Maybe this:
VBA Code:
    'Save and Close Workbook
    'wb.Close SaveChanges:=True
    'wb.Close ActiveWorkbook.SaveCopyAs("filename" & "_WL")

    wb.SaveCopyAs Filename:=Left(wb.FullName, InStrRev(wb.FullName, ".") - 1) & "_V2" & ".xlsx"
    wb.Close
 

MRxlicius

New Member
Joined
Apr 5, 2018
Messages
21
Maybe this:
VBA Code:
    'Save and Close Workbook
    'wb.Close SaveChanges:=True
    'wb.Close ActiveWorkbook.SaveCopyAs("filename" & "_WL")

    wb.SaveCopyAs Filename:=Left(wb.FullName, InStrRev(wb.FullName, ".") - 1) & "_V2" & ".xlsx"
    wb.Close
Thank you rlv01 this is working fine!!!
 

Watch MrExcel Video

Forum statistics

Threads
1,119,062
Messages
5,575,878
Members
412,689
Latest member
nhsmedic
Top