I would like copy and paste special (values & format) a range from workbook A to workbook B. The problem is : the values are paste but not the format
I've tried all the PasteSpecial, but none of it worked...
Sub Macro_copy_paste_pivot()
Dim date_report As String
Dim appExcel As Excel.Application
Dim XLBook As Workbook
Set appExcel = CreateObject("Excel.Application")
Set XLBook = appExcel.Workbooks.Add
date_report = WorksheetFunction.WorkDay(Date, -1)
date_report = Format(date_report, "yyyy-mm-dd")
' COPY and PASTE the pivot EXO
Worksheets("Pivot EXO").Activate
ActiveSheet.PivotTables("Pivot EXO").PivotFields( _
"[Context].[AsOfDate].[AsOfDate]").VisibleItemsList = Array( _
"[Context].[AsOfDate].&[" & date_report & "T00:00:00]")
Range("P7:A24").Copy
XLBook.Sheets.Add.Name = "EXO"
XLBook.Worksheets("EXO").Range("P7:A24").PasteSpecial Paste:=xlPasteFormats
End Sub
So, how can I paste format from a workbook A to workbook B?
XLBook.Worksheets("EXO").Range("P7:A24").PasteSpecial xlPasteValues
XLBook.Worksheets("EXO").Range("P7:A24").PasteSpecial xlPasteFormats
– Damian.Activate
andActiveSheet
. Instead work with the sheet directlyWorksheets("Pivot EXO").PivotTables…
• You might benefit from reading How to avoid using Select in Excel VBA. • Also always specify a sheet for all rangesRange("P7:A24").Copy
otherwise you let Excel guess wich sheet to take. Specify it likeWorksheets("Pivot EXO").Range("P7:A24").Copy
– Pᴇʜ