简体   繁体   English

调试Excel VBA ActiveWorkbook.SaveAs

[英]Debugging Excel VBA ActiveWorkbook.SaveAs

I'm not sure why a snip of VBA code I have isn't working - I'm hoping someone can help. 我不确定为什么我无法使用一小段VBA代码-我希望有人可以提供帮助。

I'm simply trying to execute a ActiveWorkbook.SaveAs as part of a larger module, but it's erroring out. 我只是试图将ActiveWorkbook.SaveAs作为更大模块的一部分执行,但是它出错了。

Here's the snippet of code where the error is occurring: 这是发生错误的代码片段:

LdrLastName = Right(wbCleaner.Sheets("Import Information").Range("$A$2"), Len(wbCleaner.Sheets("Import Information").Range("$A$2")) - Len(Left(wbCleaner.Sheets("Import Information").Range("$A$2"), LstNameSpacePos)))
ExptdQBInitialFileName = LdrLastName & " " & wbCleaner.Sheets("Import Information").Range("$D$2") 'produces "LastName AccountCode"

'ExptdQBFileName MsgBox's the full file path and file name (i.e. C:/Folder/Filename.xlsx)
ExptdQBFileName = Application.GetSaveAsFilename(InitialFileName:=ExptdQBInitialFileName, FileFilter:="QB Export File *.xlsx (*.xlsx),")

If ExptdQBFileName <> "" And ExptdQBFileName <> "False" Then
    ActiveWorkbook.SaveAs Filename:=ExptdQBFileName, _
        FileFormat:=xlOpenXMLWorkbook, _
        Password:="", _
        WriteResPassword:="", _
        ReadOnlyRecommended:=False, _
        CreateBackup:=False
End If

The error highlights everything within the If/End If statement. 该错误突出显示了If / End If语句中的所有内容。 I've used this code before without issue. 我在没有问题之前就使用过此代码。 Where am I going wrong here? 我在哪里错了?

仅出于共享目的,此工作簿与活动工作簿之间的区别http://datapigtechnologies.com/blog/index.php/thisworkbook-vs-activeworkbook/

File Format 52 is xlOpenXMLWorkbookMacroEnabled , or an xlsm file. 文件格式52是xlOpenXMLWorkbookMacroEnabledxlsm文件。 If you are saving as an .xlsx file, I think the file format you want is xlOpenXMLWorkbook , which casts to a 51. 如果要另存为.xlsx文件,我认为您想要的文件格式为xlOpenXMLWorkbook ,它将转换为51。

It's helpful to use the enumerations instead of the integers: 使用枚举而不是整数会很有帮助:

LdrLastName = Right(wbCleaner.Sheets("Import Information").Range("$A$2"), Len(wbCleaner.Sheets("Import Information").Range("$A$2")) - Len(Left(wbCleaner.Sheets("Import Information").Range("$A$2"), LstNameSpacePos)))
ExptdQBInitialFileName = LdrLastName & " " & wbCleaner.Sheets("Import Information").Range("$D$2") 'produces "LastName AccountCode"

ExptdQBFileName = Application.GetSaveAsFilename(InitialFileName:=ExptdQBInitialFileName, FileFilter:="QB Export File (*.xlsx), *.xlsx,")

If ExptdQBFileName <> "" And ExptdQBFileName <> "False" Then
    ActiveWorkbook.SaveAs Filename:=ExptdQBFileName, _
        FileFormat:=xlOpenXMLWorkbook, _
        Password:="", _
        WriteResPassword:="", _
        ReadOnlyRecommended:=False, _
        CreateBackup:=False
End If

http://www.rondebruin.nl/win/s5/win001.htm http://www.rondebruin.nl/win/s5/win001.htm

I'm not sure why but the solution was to change 我不知道为什么,但是解决方案是改变

ActiveWorkbook.("QB Export File").SaveAs

to

ThisWorkbook.Sheets("QB Export File").SaveAs

Thanks user2851376 for sharing the link below comparing ThisWorkbook vs. ActiveWorkbook http://datapigtechnologies.com/blog/index.php/thisworkbook-vs-activeworkbook/ 感谢user2851376分享了下面的链接,用于比较ThisWorkbook与ActiveWorkbook http://datapigtechnologies.com/blog/index.php/thisworkbook-vs-activeworkbook/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM