我有粘贴几个选项卡的代码。但是,我不确定如何将它们作为值粘贴,而又不会对源代码进行尽可能少的改动。
我认为当前正在使用默认的.Copy
复制到新工作簿中,因此我认为可能必须显式声明新工作簿,但不确定。
Sub ExportTabs(Boro As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'ExportTabs
'This subroutine exports separate tabs into new workbooks. It will take in a borough and export it into a workbook,
'saved in the same directory as the master workbook. The export will be a copy paste
'of values and formatting.
'
'Parameters: Boro (String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Step 1: Declare your variables
Dim MasterFile As Workbook
Dim FileName As String
Dim LeadingText As String
Dim DateT As String
'Step 2: Set file name strings, including hard-coding the header text
LeadingText = "Completion Report - "
DateT = Format(DateTime.Date, "MM-dd-yy")
'Step 3: Find active workbook and relevant sheet, and copy and save with new file name
Set MasterFile = ActiveWorkbook
FileName = MasterFile.Path & "\" & LeadingText & DateT & " " & Boro & ".xlsx"
MasterFile.Sheets(Array(Boro & " Completion Report", Boro & " Summary", "About")).Copy
ActiveWorkbook.SaveAs FileName, FileFormat:=51
'Step 4: Close new workbook
ActiveWorkbook.Close
End Sub
答案 0 :(得分:0)
我最终将Step 3
下的代码更改为此:
Set MasterFile = ActiveWorkbook
FileName = MasterFile.Path & "\" & LeadingText & DateT & " " & Boro & ".xlsx"
MasterFile.Sheets(Array(Boro & " Completion Report", Boro & " Summary", "About")).Copy
For Each sht In ActiveWorkbook.Sheets
'Only copy and paste as values in Summary tab
If sht.Name = Boro & " Summary" Then
sht.UsedRange.Copy
sht.UsedRange.PasteSpecial xlValues
sht.UsedRange.PasteSpecial xlFormats
End If
Next sht
ActiveWorkbook.SaveAs FileName, FileFormat:=51
我发现.Copy
复制到Excel识别为ActiveWorkbook
的新工作簿中,因此您可以从那里更改工作簿。粘贴为值的唯一方法似乎是.PasteSpecial xlValues
方法。