简体   繁体   English

1004错误-SaveAs问题

[英]1004 Error- issues with SaveAs

savePath = "\\local drive path"

myFileName = savePath & "Workbook Name " & saveDate

Application.DisplayAlerts = False

'MsgBox (myFileName)

wbTarget.Activate

ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook

When using the above code, I get an error 1004 "Method SaveAs of object_Workbook failed and can't figure out why. I'm relatively new to excel but belive I have everything set up right. 使用上面的代码时,出现错误1004“ object_Workbook的方法SaveAs失败,无法弄清原因。我对excel还是比较陌生,但是我已经正确设置了所有内容。

Update: Sorry should have shown the more complete code. 更新:对不起,应该已经显示了更完整的代码。

Sub SaveAs()


'Save As "Workbook Name"

Dim wbTarget            As Workbook
Dim wbThis              As Workbook
Dim strName             As String

Set wbTarget = Workbooks("Workbook Name")

wbTarget.Activate

Dim myFileName As String
Dim saveDate As String
Dim saveMonth As String
Dim monthNum As String
Dim savePath As String

Sheets("Raw").Select

saveDate = Range("A2").Value
saveMonth = Range("A2").Value
monthNum = Range("A2").Value


savePath = "\\local drive path"

myFileName = savePath & "Riskviews Data " & saveDate

Application.DisplayAlerts = False

'MsgBox (myFileName)

wbTarget.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook



End Sub

I updated the last part to say "wbTarget.SaveAs" but that didn't seem to work. 我将最后一部分更新为“ wbTarget.SaveAs”,但这似乎不起作用。

The problem may be with ActiveWorkBook . 问题可能出在ActiveWorkBook You should try to avoid relying on .Active , .Activate , and .Select when you can simply refer to your objects directly. 当您可以直接直接引用对象时,应避免依赖.Active.Activate.Select

Delete wbTarget.Activate and try the below, 删除wbTarget.Activate并尝试以下操作,

WbTarget.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook

I can see you have tried to debug. 我可以看到您已尝试调试。 MsgBox myFileName (notice: no () ) should have displayed what you are passing to the save command. MsgBox myFileName (注意:no () )应该已经显示了您要传递给save命令的内容。 From your code above, it would be \\\\local drive pathWorkbook Name - noting that there is a space at the end as well. 从上面的代码中,它将是\\\\local drive pathWorkbook Name注意末尾也有一个空格。 You have not defined saveDate , you have not added an extension and you have not indicated the file format. 您尚未定义saveDate ,尚未添加扩展名,也未指定文件格式。

See the following answers for further information to SaveAs . 有关SaveAs更多信息,请参见以下答案。

How to do a "Save As" in vba code, saving my current Excel workbook with datestamp? 如何在vba代码中执行“另存为”,并用datestamp保存当前的Excel工作簿?

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

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