带范围的活动工作表副本

时间:2016-08-21 08:57:13

标签: excel vba excel-vba

今天我需要复制带有范围的活动表,而不是所有页面。

我现在有这个代码:

Sub Save1()
 Dim NomFichier, Nom
 Nom = Day(Date) & "-" & Month(Date) & "-" & Year(Date) & "_" & Hour(Time) & "-" & Minute(Time) & "-" & Second(Time)

ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\" & "Chambre_" & ActiveSheet.Name & "_" & Nom

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\PDF\" & "Chambre_" & ActiveSheet.Name & "_" & Nom, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
From:=1, To:=1, OpenAfterPublish:=False

End Sub
Sub Reinitialiser()
    [D4:J53].ClearContents
End Sub

但是当我更改此行时:ActiveSheet.CopyActiveSheet.Range("A1:J72").Copy副本可以正常工作,但是会创建一个包含所有工作表的工作簿。

如果只保存具有良好范围的活动表,我该怎么办?

2 个答案:

答案 0 :(得分:1)

以下代码行:

## Controller add this code snipped
   get_feature = Product.find(...).features
   rating(get_feature)

   protected

   def rating(get_all_feature)
     all_rating = []
     get_all_feature.each do |feature|
       all_rating <<  feature.product_features.each { |u| u.rating }
     end
     all_rating
   end

会将ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\PDF\" & "Chambre_" & ActiveSheet.Name & "_" & Nom, _ Quality:= xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ From:=1, To:=1, _ OpenAfterPublish:=False 导出为PDF文件。

要导出只是 ActiveSheet,您应该说:

ActiveSheet.Range("A1:J72")

您的代码行

ActiveSheet.Range("A1:J72").ExportAsFixedFormat _
                                Type:=xlTypePDF, _
                                Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\PDF\" & "Chambre_" & ActiveSheet.Name & "_" & Nom, _
                                Quality:= xlQualityStandard, _
                                IncludeDocProperties:=True, _
                                IgnorePrintAreas:=False, _
                                From:=1, To:=1, _
                                OpenAfterPublish:=False
执行导出不需要

。 (出于其他原因可能需要SaveAs,例如,因为您希望将整个工作簿的副本保存在一个目录中,将PDF版本保存在子目录中。绝对不需要复制。)

答案 1 :(得分:0)

要使用Range.Copy方法,您需要指定要将数据复制到的目标范围。我已经使用注释更新了下面的代码,以显示如何将Range复制到新工作簿中的新工作表。您需要将“WorksheetName”更改为包含您的数据的工作表的名称:

Sub Save1()

Dim wb As Excel.Workbook, wsSource As Excel.Worksheet, wsDest As Excel.Worksheet
Dim NomFichier, Nom
Nom = Day(Date) & "-" & Month(Date) & "-" & Year(Date) & "_" & Hour(Time) & "-" & Minute(Time) & "-" & Second(Time)

' Reference the Worksheet containing the data to be copied
' Change "WorksheetName" to the name of the Worksheet to copy from
Set wsSource = ActiveWorkbook.Worksheets("WorksheetName")

' Create a new Workbook with one Worksheet
Set wb = Workbooks.Add(xlWBATWorksheet)

' Set a reference to the Worksheet in the new Workbook
Set wsDest = wb.Worksheets(1)

' Copy the data to the new Worksheet, paste at Cell "A1"
wsSource.Range("A1:J72").Copy wsDest.Range("A1")
wb.SaveAs Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\" & "Chambre_" & wsSource.Name & "_" & Nom

wsDest.ExportAsFixedFormat Type:=xlTypePDF, _
                        Filename:="C:\Users\Illyana\Documents\Dropbox\Personnel\Freddy\Audit réalisé\Chambre 100\PDF\" & "Chambre_" & wsSource.Name & "_" & Nom, Quality:= _
                        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                        From:=1, To:=1, OpenAfterPublish:=False

End Sub