Excel VBA 删除公式并另存为 v2
Posted
技术标签:
【中文标题】Excel VBA 删除公式并另存为 v2【英文标题】:Excel VBA to remove formulas and save as v2 【发布时间】:2020-04-16 15:01:11 【问题描述】:在下面的代码中,我正在尝试调整,所以我循环浏览选项卡上的所有文件,如果范围有公式将其转换为值。但仅适用于特定选项卡。在关闭和保存文件之前,我想将其保存为当前名称并在末尾添加“_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
【问题讨论】:
您最好确定相关范围,然后将整个内容作为值复制/粘贴回来,而不是逐个单元格地进行此操作 那么你的意思是你只想在工作表Cubes Act '20
上运行这个工作表(如果该工作表存在)?如果是这样,有 UDF 可以检查是否存在可用于清理代码的工作表
【参考方案1】:
我相信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
【讨论】:
感谢您的回答。这确实应该注意文件的命名。以上是关于Excel VBA 删除公式并另存为 v2的主要内容,如果未能解决你的问题,请参考以下文章