VBA Excel-保存文件时过程调用或参数无效

VBA Excel-保存文件时过程调用或参数无效,excel,vba,Excel,Vba,早上好 我面临以下问题。 当我尝试在新版本下保存文件时,我会不时收到错误,其中说明: 无效的过程调用或参数-sChangeVersiojn 请参见下面的我的代码: Sub Version_save() ' Saving file under the newer version wothout changing its name On Error GoTo E_Handle Dim strFileName As String Dim strFileExt As String str

早上好

我面临以下问题。 当我尝试在新版本下保存文件时,我会不时收到错误,其中说明: 无效的过程调用或参数-sChangeVersiojn

请参见下面的我的代码:

 Sub Version_save()
 ' Saving file under the newer version wothout changing its name
 On Error GoTo E_Handle
 Dim strFileName  As String
 Dim strFileExt As String
 strFileName = ThisWorkbook.Name
 strFileExt = Mid(strFileName, InStrRev(strFileName, "."))
 strFileName = Left(strFileName, InStrRev(strFileName, ".") - 1) '   get rid of the file extension (it should be .xlsm)
 strFileName = Left(strFileName, InStrRev(strFileName, "V") - 1) '   now go back to the last "V" in the file name
 strFileName = strFileName & "V" & ThisWorkbook.Worksheets("Frontsheet").Range("D38") & ".0" & strFileExt
 ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & strFileName
 Debug.Print strFileName
 Debug.Print strFileExt
 sExit:
     On Error Resume Next
     Exit Sub
 E_Handle:
     MsgBox Err.Description & vbCrLf & vbCrLf & "sChangeVersiojn", vbOKOnly + vbCritical, "Error: " & 
     Err.Number
     Resume sExit
 End Sub
根据以下线索:

我应该更改引号,但我真的不知道应该在哪里做


有没有办法消除此错误?

您的代码假定您的文件名与某个模式匹配,如果不匹配,则继续提供一条没有帮助的错误消息

您可以测试文件名是否与模式匹配,如果不匹配,则采取适当的操作。如果是,请继续并更改名称并另存为

将其添加到Sub的顶部,并更改图案以满足您的需要

'Test if file name contains a "V", and not as first or last character in the name.  Also require a .xlsm or .xlsx extension
If Not ThisWorkbook.Name Like "*?V?*.xls[mx]" Then
    MsgBox "File Name """ & ThisWorkbook.Name & """ does not match required pattern", vbOKOnly, "Error"
    ' File name does not conform.  Now What?
    GoTo sExit
End If

删除错误句柄并查看错误发生的位置。在错误转到0时使用
,这样您至少可以确定中断代码的行'This
strFileName=Left(strFileName,instrev(strFileName,“.”)-1)
或This
strFileName=Left(strFileName,instrev(strFileName,“V”)-1)
可能是原因,当文件名不包含“.”或“V”时,您是对的。可能是这样的。有什么办法可以克服它吗?问题是,我的一些作品同时包含“.”和“V”,而有些作品不包含“.”和“V”。我需要一些IF声明或案例。太好了!谢谢我已经关闭了GoTo Exit,并将其替换为另存为应用程序对话框。现在,解决方案非常适合我!如果模式与代码不匹配,我可以直接手动保存它!