SaveAs将不接受包含“."的字符串.在Excel VBA中 [英] SaveAs won't accept strings that contain "." in Excel VBA

查看:71
本文介绍了SaveAs将不接受包含“."的字符串.在Excel VBA中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来添加新工作簿,保存并命名工作簿(基于位于工作表中某个单元格中的日期).

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

但是似乎每当单元格"M145"像"31.3.16"中包含点(.")时,我的文件名都不会出现在另存为"提示中,并且我看到一个空白行,没有 任何错误消息.

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 without any 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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆