【发布时间】:2015-08-02 08:02:45
【问题描述】:
诚然,我不擅长理解行话,所以虽然我认为我对此进行了彻底的研究,但在某个地方可能会有完美的答案。这是我的困境,我正在开发这个 Excel VBA 宏来备份和恢复工作表(基本上给我无限的撤消到我指定的点以及保存和重新打开的捷径):
Public BULast As String
Sub Backup()
'This macro imitates videogame save-states. It will save a backup that can replace to current workbook later if you've made an irreversible mistake.
'Step 1: Agree to run away if things go wrong (establish an error handler)
On Error GoTo BackupError
'Step 2: Create some variables
Dim OriginalFile As String
Dim BUDir As String
Dim BUXAr() As String
Dim BUExt As String
Dim BUNam As String
Dim BackupFile As String
'Step 3: Define those variables
OriginalFile = ActiveWorkbook.FullName
BUDir = ActiveWorkbook.Path
BUXAr = Split(ActiveWorkbook.FullName, ".")
BUExt = BUXAr(UBound(BUXAr))
BUNam = Replace(ActiveWorkbook.Name, "." & BUExt, "") & " (Back-Up)"
BackupFile = BUDir & "\" & BUNam & "." & BUExt
'Step 4: Hide the truth
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Step 5(A): If there is no backup file, create one using the same file name as the one you're working with and throw a " (Back-up)" on it.
If Dir(BackupFile) = "" Then
ActiveWorkbook.SaveAs filename:=BackupFile
ActiveWorkbook.Close
Workbooks.Open filename:=OriginalFile
BUYoN = vbNo
BULast = Date & ", " & Time
MsgBox "A Backup has been created!"
Else
BUYoN = MsgBox("This will restore the " & BULast & " backup and undo all changes made to this project. Continue?" _
, vbYesNo, "Revert to Backup?")
End If
'Step 5(B): If a backup has been created, restore it over the current workbook and delete the backup.
If BUYoN = vbYes Then
ActiveWorkbook.Close
Workbooks.Open filename:=BackupFile
ActiveWorkbook.SaveAs filename:=OriginalFile
Kill (BackupFile)
BUCheck = "Dead"
End If
'Step 6: Put things back to the way you found them, you're done!
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
'Step 1 (Continued): If nothing went wrong, stop worrying about it, if something did, say it didn't work and go away.
On Error GoTo 0
BackupError:
MsgBox "Attempt to Backup or Restore was unsuccessful"
End Sub
通常它可以按预期工作,但就在昨天它开始无法正常工作,在玩弄它之后我意识到这是因为我在文件名中有 Ω 符号的文件上尝试它。
基本过程是在当前目录中查找活动工作簿的文件名,但在末尾添加(备份)。它要么创建一个,要么用它找到的替换打开的。然而,当在 Ω 文件上完成时,它会用 O 替换该字符。当再次运行时,它显然 搜索 Ω 正确,因为它找不到任何(即使使用 O-substitute 文件正确就在眼前)。
我知道最简单的解决方案是确保人们将他们的文件名保留在您在键盘上可以看到的内容,但这对我不起作用;我几乎热衷于将适应性放在代码而不是用户中。所以有了这个冗长的背景故事,这是我的具体问题:
VBA 中是否有可以处理指定文件名中的特殊字符的 SaveAs 函数或实用解决方法?
【问题讨论】: