Home > front end >  VBA How To Paste With Blank One Column Over?
VBA How To Paste With Blank One Column Over?

Time:01-26

How do I edit my code to paste with a blank column between each paste? This is from a loop of each filter and pasting to another sheet. The code below works but it starts at C column when I want it to start at B.

Set shtcopy = Sheets("Summary Copying")
Set shtpaste = Sheets("Summary")
        
        shtcopy.PivotTables
            pt.TableRange1.Copy
            shtpaste.Cells(10, Columns.Count).End(xlToLeft).Offset(, 2).PasteSpecial Paste:=xlPasteValues
            shtpaste.Cells(10, Columns.Count).End(xlToLeft).Offset(, -4).PasteSpecial Paste:=xlPasteFormats
        shtpaste.Cells.Columns.AutoFit

CodePudding user response:

Option Explicit

Sub CopyPaste()

    Dim pt As PivotTable, wb As Workbook, n As Long
    Dim shtCopy As Worksheet, shtpaste As Worksheet
    
    Set wb = ThisWorkbook
    With wb
        Set shtCopy = .Sheets("Summary Copying")
        Set shtpaste = .Sheets("Summary")
    End With
         
    Set pt = shtCopy.PivotTables(1)
    pt.TableRange1.Copy
         
    With shtpaste
        n = .Cells(10, .Columns.Count).End(xlToLeft).Column
        If n < 2 Then
            n = 2
        Else
            n = n   2
        End If
    
        .Cells(10, n).PasteSpecial Paste:=xlPasteValues
        .Cells(10, n).PasteSpecial Paste:=xlPasteFormats
        .Cells.Columns.AutoFit
    End With
  
End Sub
  • Related