另存为不会在Excel VBA中包含“。”的string

我正在使用下面的代码为了添加一个新的工作簿,保存并命名该工作簿(基于位于工作表中某个单元格中的date)。

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 

但是,似乎在“M145”单元中包含像“31.3.16”中的圆点(“。”)时,我的文件名不会出现在SaveAs提示符中,并且我看到一个没有任何错误信息的空白行。

我不认为这与它有什么关系,但我的表是从右到左。 有没有人有如何解决这个问题的想法?

虽然我无法复制这个错误,但是用FileDialog对象也许会有更好的运气:

 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 

这里有两件事。

首先,M145可能包含一个格式掩码为dd\.mm\.yy 。 为了将显示值从单元格变成variables,需要Range.Text属性 ,而不是Range.Value属性 。

其次, Application.GetSaveAsFilename方法的默认文件types是*.* ,这意味着它将接受.yy作为文件扩展名。 您需要将可用的文件扩展名限制为Excel文件types。

 Dim wb As Workbook Dim wbName As String, fName As Variant 'variant in case user clicks Cancel wbName = ThisWorkbook.Sheets("Sheet1").Range("M145").Text With Application fName = .GetSaveAsFilename(InitialFileName:=wbName, _ FileFilter:="Excel Workbook (*.xlsx), *.xlsx," & _ "Macro Workbook (*.xlsm), *.xlsm," & _ "Binary Workbook (*.xlsb), *.xlsb") End With 

这让你通过你的文件名select的问题。 但是, Workbook.SaveAs方法也应该提供正确的XlFileFormat枚举 。

 Select Case fName Case False 'user clicked Cancel or Close (×) Debug.Print fName Case Else With Workbooks.Add Select Case Right(fName, 5) Case ".xlsx" .SaveAs Filename:=fName, FileFormat:=xlOpenXMLWorkbook Case ".xlsm" .SaveAs Filename:=fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled Case ".xlsb" .SaveAs Filename:=fName, FileFormat:=xlExcel12 Case Else 'there really shouldn't be a case else End Select End With End Select