VBA - problems with pasting formats

thorn91

New Member
Joined
Jun 4, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello

For some reason, my code only allows me to to use "Paste" or "Paste Special xlPasteValuesAndNumberFormats". In case I try to use "PasteSpecial xlPasteFormats", I'm getting 1004 error. I can't wrap my mind around what I might be doing wrong. Preferably I would need to paste both formats of the source file as well as values.

A piece of code causing the problem:

VBA Code:
Private Sub SplitWorksheet(ByVal Category_Name As Variant)
    
    Dim wbTarget As Workbook
    Dim Target_Folder As String
    Dim column_name_to_use_2 As String
    Dim Prefix As String
    
    
    column_name_to_use_2 = wsHelper.Range("E1").Value
    
    Target_Folder = wsHelper.Range("F1").Value
    Prefix = wsHelper.Range("G1").Value
    
    Set wbTarget = Workbooks.Add
    
    With wsSource
        
        With .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
            .AutoFilter .Range(column_name_to_use_2).Column, Category_Name
            
            .Copy
            
            wbTarget.Worksheets(1).PasteSpecial xlPasteFormats
            wbTarget.Worksheets(1).PasteSpecial xlPasteValuesAndNumberFormats
            wbTarget.Worksheets(1).Name = Category_Name
            
            wbTarget.SaveAs Target_Folder & Prefix & "_" & Category_Name & ".xlsx", 51
            wbTarget.Close False
            
        End With
        
    End With
    
    Set wbTarget = Nothing


End Sub

If I remove "wbTarget.Worksheets(1).PasteSpecial xlPasteFormats" completely, it works just fine. But if I leave it like above, I get an error.

Would be very much grateful for any ideas.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
It is because you are using the Worksheet.PasteSpecial method instead of the Range.PasteSpecial method. Try using the range pastespecial method instead.

VBA Code:
            wbTarget.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteFormats
            wbTarget.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
 
Upvote 0
Solution
It is because you are using the Worksheet.PasteSpecial method instead of the Range.PasteSpecial method. Try using the range pastespecial method instead.

VBA Code:
            wbTarget.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteFormats
            wbTarget.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
This worked, thanks so much
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top