我正在尝试在excel功能区上创建一个重复按钮,当单击它时,将创建活动工作簿的重复文件,但我希望它创建一个重复文件,只有前两个页面复制在重复文件中而不是整个活跃的工作簿。
我尝试使用以下代码获取重复文件:
Sub DupliquerFeuille(control As IRibbonControl)
Dim Sourcewb As Workbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
nom = Day(Date) & "-" & Month(Date) & "-" & Year(Date) & "_" & ActiveWorkbook.Name
ActiveWorkbook.SaveCopyAs ActiveWorkbook.Path & "\" & nom
' ActiveWorkbook.Sheets(Array(1, 2)).Copy
End Sub
但是我在重复文件中复制了所有活动工作簿。任何人都可以帮助我只获取复制文件中复制的第一张活动工作簿。我尝试了很多,但我无法得到结果。
答案 0 :(得分:1)
试试这个:
Sub DupliquerFeuille(control As IRibbonControl)
Dim twb As Workbook
Dim Sourcewb As Workbook
Const shc As Long = 2 ' change this as you need, this will copy first 2 sheets
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
Sourcewb.Sheets(1).Copy
Set twb = ActiveWorkbook
For i = 2 To shc
Sourcewb.Sheets(i).Copy ,twb.Sheets(twb.Sheets.Count)
Next
nom = Day(Date) & "-" & Month(Date) & "-" & Year(Date) & "_" & Sourcewb.Name
twb.SaveAs Sourcewb.Path & "\" & nom, Sourcewb.FileFormat
twb.Close False
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
答案 1 :(得分:0)
在代码行下面找到当前excel文件的路径和名称,复制前两张并保存到与主工作簿相同位置的新(重复)工作簿:
graphData
答案 2 :(得分:0)
在保存新工作簿以删除那些你不喜欢的页面之后,只需添加几行代码怎么样?
如
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
替换"表1和#34;使用工作表的名称(保留引号)