gpt4 book ai didi

excel - 复制一堆工作表并使用原始工作表的范围保存?

转载 作者:行者123 更新时间:2023-12-04 20:50:32 25 4
gpt4 key购买 nike

我想在我的工作簿中复制此工作表并使用原始工作表的 sheet1 的单元格 C29 中的值将其保存为 CSV:Del Mar_Replines_20200831_Macro Build.xlsm。当我运行它时,我得到一个“下标超出范围”错误。
如果我只是将单元格 C29 中的值粘贴为文件名,则宏可以工作。单元格 C29 是一个方程式(不确定这是否会有所不同)。

Sheets("Balance Sheet").Copy

ActiveWorkbook.SaveAs Filename:=Workbooks("Del Mar_Replines_20200831_Macro Build").Worksheets("Sheet1").Range("C29").Value

ActiveWindow.Close

最佳答案

此宏假定两个工作表位于 Workbooks("Del Mar_Replines_20200831_Macro Build")并且您的宏在同一个工作簿中。
第一:您需要确保C29中的值不包含任何不能在文件名中的非法字符。

With ThisWorkbook 'refers to the workbook that contains your macro
.Sheets("Balance Sheet").Copy 'the sheet you want to copy

'When you copy a sheet it becomes active. If you don't identify the save path,
'the active workbook will be saved to Excels default save location.
'The new saved workbook will be located in Excels default save location;
'the file name will be the value from "ThisWorkbook.Sheets("Sheet1").(C29)",
'with file extension ".CSV", and file format "6"(csv).

ActiveWorkbook.SaveAs .Sheets("Sheet1").Range("C29").Value & ".csv", FileFormat:=6

ActiveWindow.Close SaveChanges:=False 'close the new workbook with no popup
End With

关于excel - 复制一堆工作表并使用原始工作表的范围保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64033971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com