Save a file with a name that corresponds to a cell value

时光总嘲笑我的痴心妄想 提交于 2021-01-28 19:49:59

问题


Background: I'm working with a macro that transfers data between different workbooks. One of the workbooks is a conduit for data and doesn't change. The other workbook is a blank form that is populated, saved as a specific name and then reused.

Problem: The workbook being saved needs to use the contents of a cell in the conduit workbook for it's name.

I found a similar post here. The answer gives me a syntax error.

Sub TestTestTestTest()
'
' TestTestTestTest Macro
'

'
    Dim name As String
    name = Cells("Contents", A1)
    ActiveWorkbook.SaveAs() Filename:=name**********
End Sub

The starred line is the source of the syntax error.


回答1:


Have you considered the file path? or which workbook you are getting A1 from?

I assume Contents is a sheet and A1 is the filename ( for future people looking at this Contents was not the sheet)

Dim name As String
name = range("A1").value
ActiveWorkbook.SaveAs Filename:=name

just for your knowledge to do the same with cells you can use

cells(1,1).value

Cells expects numerical values for column/row

To point to a sheet you can use

sheets("sheet1").range("A1").value
sheets("sheet1").cells(1,1).value

You could shorten this whole thing by using

ActiveWorkbook.SaveAs Filename:=range("A1").value



回答2:


You have to remove brackets:

ActiveWorkbook.SaveAs Filename:=name

Or if you want to use bracket, you need to put parameters inside them and add keyword Call at the beginning:

Call ActiveWorkbook.SaveAs(Filename:=name)


来源:https://stackoverflow.com/questions/31433489/save-a-file-with-a-name-that-corresponds-to-a-cell-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!