问题描述
我正在使用以下代码来添加新的工作簿,保存并命名工作簿(根据工作表中某个单元格中的日期)。
I am using the following code in order to add a new workbook,save and name the workbook (based on a date which is located in a certain cell in the sheet).
Dim wb As Workbook
Dim wbName As String
wbName = ThisWorkbook.Sheets("Sheet1").Range("M145").value
fName = Application.GetSaveAsFilename(wbName)
If fName = False Then
MsgBox "Publish couldn't be completed since you didn't choose where to save the file."
Exit Sub
Else
Set wb = Workbooks.Add
wb.SaveAs (fName)
End If
但是,像31.3.16一样,只要单元格M145包含点(。),我的文件名不会显示在SaveAs提示,我看到一个空行,没有
任何错误消息。
But it seems that whenever cell "M145" contains dots (".") as in "31.3.16", my file name doesn't appear in the SaveAs prompt and I see a blank line withoutany error message.
我不认为这与它有任何关系,但我的表是正确的-向左。有没有人想到如何解决这个问题?
I don't think that this has anything to do with it, but my sheet is right-to-left. Does anyone has an idea on how to fix this?
推荐答案
虽然我无法复制错误,有更好的运气与一个 FileDialog
对象:
While I'm not able to replicate the error, perhaps you will have better luck with a FileDialog
object:
Dim wb As Workbook
Dim wbName As String
Dim fdlg As FileDialog
wbName = ThisWorkbook.Sheets("Sheet1").Range("M145").value
Set fdlg = Application.FileDialog(msoFileDialogSaveAs)
With fdlg
.InitialFileName = wbName
.Show
Set wb = Workbooks.Add
On Error Resume Next 'Suppress any errors due to invalid filename, etc.
wb.SaveAs(fdlg.SelectedItems(1))
If Err.Number <> 0 Then
MsgBox "Publish couldn't be completed since you didn't choose where to save the file."
wb.Close False 'Get rid of the workbook since it's not being saved
Exit Sub
End If
On Error GoTo 0 'Resume normal error handling
End With
这篇关于SaveAs不会包含包含“。”的字符串。在Excel VBA中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!