Excel VBA 删除公式并另存为 v2
Excel VBA to remove formulas and save as v2
在下面的代码中,我试图进行调整,因此我循环遍历选项卡上的所有文件,如果范围有将其转换为值的公式。但仅适用于特定选项卡。在关闭并保存文件之前,我想将其保存为当前名称并在末尾添加“_v2”。有人可以帮忙吗?
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" & "_v2")
'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
我相信函数 NextFileName()
可以满足您的需求。
我几乎总是在我的文件上有版本号,所以当我完成更新文件“AAA V27.xxx”时,我经常需要找到新名称“AAA V28.xxx”。我将函数 NextFileName()
保留在 PERSONAL.XLSB 中,这样我就可以从我的任何工作簿中调用它。
这里NextFileName()
的版本不是我最新的版本。一年前,我遇到了根据日期处理文件版本的需求,并创建了一个新版本的 NextFileName()
来处理此类文件名。如果找不到版本号,我找到了我的旧版本并对其进行了增强以添加“V2”。
您可以运行 TestNextFileName()
来检查我的例程是否如您所愿。
Option Explicit
Sub TestNextFileName()
Debug.Print ("AAA 5.abc -> " & NextFileName("AAA 5.abc"))
Debug.Print ("BBB V9.abc -> " & NextFileName("BBB V9.abc"))
Debug.Print ("CCC V05.abc -> " & NextFileName("CCC V05.abc"))
Debug.Print ("DDD V1.99.abc -> " & NextFileName("DDD V1.99.abc"))
Debug.Print ("EEE.abc -> " & NextFileName("EEE.abc"))
End Sub
Public Function NextFileName(ByVal CrntFileName As String) As String
' * CrntFileName should be of the format "zzzzyy.xxx" where
' yy represents a string of one or more decimal digits
' the final z is anything but a decimal digit.
' xxx represents the extension
' * If CrntFileName is of this format, the routine returns "zzzzww.xxx" where
' ww is one more than yy.
' * If CrntFileName is not of this format. the routine returns "zzzz V2.xxx".
' * Examples:
' AAA 5.abc -> AAA 6.abc
' BBB V9.abc -> BBB V10.abc
' CCC V05.abc -> CCC V06.abc
' DDD V1.99.abc -> DDD V1.100.abc
' EEE.abc -> EEE V2.abc
' 26Jul11 Coded and tested under VB 2010.
' 22Nov11 Amended for VBA
' 14Jan19 Became obsolete when version that allowed for other options coded.
' 16Apr20 Resurrected in response to SO question. Amended to add V2 if no
' version number found
Dim Extn As String
Dim Name As String
Dim Pos As Integer
Dim Version As String
' Split CrntFileName into name and extension
Pos = InStrRev(CrntFileName, ".")
If Pos = 0 Then
Name = CrntFileName
Extn = ""
Else
Name = Mid(CrntFileName, 1, Pos - 1)
Extn = Mid(CrntFileName, Pos) ' Includes dot
End If
Pos = Len(Name)
Do While True
If IsNumeric(Mid(Name, Pos, 1)) Then
Pos = Pos - 1
Else
Pos = Pos + 1
Exit Do
End If
Loop
' If Pos > Len(Name), there are no trailing digits
' if Pos <=Len(Name), Pos identifies the first or only trailing digit.
If Pos > Len(Name) Then
NextFileName = Name & " V2" & Extn
Else
Version = Mid(Name, Pos) + 1 ' Next version number
' Add leading zeros if necessary to pad to original length
Do While Len(Version) < Len(Mid(Name, Pos))
Version = "0" & Version
Loop
NextFileName = Mid(Name, 1, Pos - 1) & Version & Extn
End If
End Function
在下面的代码中,我试图进行调整,因此我循环遍历选项卡上的所有文件,如果范围有将其转换为值的公式。但仅适用于特定选项卡。在关闭并保存文件之前,我想将其保存为当前名称并在末尾添加“_v2”。有人可以帮忙吗?
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" & "_v2")
'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
我相信函数 NextFileName()
可以满足您的需求。
我几乎总是在我的文件上有版本号,所以当我完成更新文件“AAA V27.xxx”时,我经常需要找到新名称“AAA V28.xxx”。我将函数 NextFileName()
保留在 PERSONAL.XLSB 中,这样我就可以从我的任何工作簿中调用它。
这里NextFileName()
的版本不是我最新的版本。一年前,我遇到了根据日期处理文件版本的需求,并创建了一个新版本的 NextFileName()
来处理此类文件名。如果找不到版本号,我找到了我的旧版本并对其进行了增强以添加“V2”。
您可以运行 TestNextFileName()
来检查我的例程是否如您所愿。
Option Explicit
Sub TestNextFileName()
Debug.Print ("AAA 5.abc -> " & NextFileName("AAA 5.abc"))
Debug.Print ("BBB V9.abc -> " & NextFileName("BBB V9.abc"))
Debug.Print ("CCC V05.abc -> " & NextFileName("CCC V05.abc"))
Debug.Print ("DDD V1.99.abc -> " & NextFileName("DDD V1.99.abc"))
Debug.Print ("EEE.abc -> " & NextFileName("EEE.abc"))
End Sub
Public Function NextFileName(ByVal CrntFileName As String) As String
' * CrntFileName should be of the format "zzzzyy.xxx" where
' yy represents a string of one or more decimal digits
' the final z is anything but a decimal digit.
' xxx represents the extension
' * If CrntFileName is of this format, the routine returns "zzzzww.xxx" where
' ww is one more than yy.
' * If CrntFileName is not of this format. the routine returns "zzzz V2.xxx".
' * Examples:
' AAA 5.abc -> AAA 6.abc
' BBB V9.abc -> BBB V10.abc
' CCC V05.abc -> CCC V06.abc
' DDD V1.99.abc -> DDD V1.100.abc
' EEE.abc -> EEE V2.abc
' 26Jul11 Coded and tested under VB 2010.
' 22Nov11 Amended for VBA
' 14Jan19 Became obsolete when version that allowed for other options coded.
' 16Apr20 Resurrected in response to SO question. Amended to add V2 if no
' version number found
Dim Extn As String
Dim Name As String
Dim Pos As Integer
Dim Version As String
' Split CrntFileName into name and extension
Pos = InStrRev(CrntFileName, ".")
If Pos = 0 Then
Name = CrntFileName
Extn = ""
Else
Name = Mid(CrntFileName, 1, Pos - 1)
Extn = Mid(CrntFileName, Pos) ' Includes dot
End If
Pos = Len(Name)
Do While True
If IsNumeric(Mid(Name, Pos, 1)) Then
Pos = Pos - 1
Else
Pos = Pos + 1
Exit Do
End If
Loop
' If Pos > Len(Name), there are no trailing digits
' if Pos <=Len(Name), Pos identifies the first or only trailing digit.
If Pos > Len(Name) Then
NextFileName = Name & " V2" & Extn
Else
Version = Mid(Name, Pos) + 1 ' Next version number
' Add leading zeros if necessary to pad to original length
Do While Len(Version) < Len(Mid(Name, Pos))
Version = "0" & Version
Loop
NextFileName = Mid(Name, 1, Pos - 1) & Version & Extn
End If
End Function