简体   繁体   English

Excel VBA导出CSV,其路径/文件名取自不同工作表中的单元格

[英]Excel VBA export CSV with path/filename taken from cell in a different sheet

I have the following code which saves the active sheet in a workbook to a csv file 我有以下代码将工作簿中的活动工作表保存到csv文件中

   ActiveWorkbook.SaveAs FileName:=Range("T1").Value & ".csv", FileFormat:=xlCSV

the filename is taken from cell T1 in the active sheet. 文件名取自活动工作表中的单元格T1。 I would like to specify the filename in a different sheet -- say cell A2 in Sheets("path") 我想在其他工作表中指定文件名-在Sheets(“ path”)中说单元格A2

I have searched for the answer, but I can't find how to reference it correctly. 我已经搜索了答案,但是找不到正确引用的方法。

Any help would be much appreciated. 任何帮助将非常感激。

Use 采用

ActiveWorkbook.SaveAs FileName:=Workbooks(nameOfWorkbook).Sheets(nameOfSheet).Range("T1").Value & ".csv", FileFormat:=xlCSV

Where 哪里

nameOfWorkbook
nameOfSheet

are strings like "myWorkbook" and "Sheet2" 是“ myWorkbook”和“ Sheet2”之类的字符串

If you want a sheet in the current workbook, you could use 如果要在当前工作簿中使用工作表,则可以使用

ActiveWorkbook.Sheets(...)

or even 甚至

Sheets(...)

since the default is "this one". 因为默认值为“ this one”。 But I highly recommend being explicit - with VBA you never know... 但是我强烈建议您保持露骨-使用VBA,您永远不会知道...

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

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