复制一堆纸并保存原始纸的使用范围?

时间:2020-09-23 18:14:04

标签: excel vba

我想复制此工作表到我的工作簿中,并使用我复制出来的原始工作表的工作表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

1 个答案:

答案 0 :(得分:0)

此宏假定两个工作表位于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