Copy and Paste Values Error

NorthbyNorthwest

Board Regular
Joined
Oct 27, 2013
Messages
173
Office Version
  1. 365
Hi, everyone. I have a sub routine that loops through about a dozen worksheets, copies a specific range from one sheet and pastes the values only to another sheet. The sub was working fine initially and is now gagging on one line of code. I don't know why. The error happens on the pastespecial paste values line of code. It reads: Addme.PasteSpecial xlPasteValues

Can you someone tell me why this is happening and how it can be rewritten?

Code:
Sub CombineMonths()
Dim ws As Worksheet
Dim Addme As Range
Dim Copyto As Range
Dim rng As Range
Dim Area As Range
Dim CombineSort As Worksheet
'Stop screen flicker
Application.ScreenUpdating = False
'Find the next row to add the data to
Set Addme = Sheet23.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
'Clear all the values
Sheet23.Unprotect
Sheet23.Range("B7:CJ3007").ClearContents
'Loop through worksheets
For Each ws In Worksheets
    'This is the range we will be copying
    Set Copyto = ws.Range("A7:CI106")
    'Use the code name in case sheet name changes
        Select Case ws.CodeName
            'Exclude these sheets by code name
            'On Error Resume Next
            Case "Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9", "Sheet10", _
            "Sheet23", "Sheet24", "Sheet25", "Sheet26", "Sheet27", "Sheet28", "Sheet29", "Sheet30", "Sheet31", "Sheet32", _
            "Sheet33", "Sheet34", "Sheet35", "Sheet36", "Sheet37", "Sheet38"
            'Add the rest
            Case Else
            'Check if ws protected, if it is protected unprotect
                If SheetProtected(ws) Then
                    'If protected
                    ws.Unprotect
                    Else
                    'If not protected
                End If
        'Sort the value in case a row is deleted
        ws.Range("B7:CJ3007").Sort Key1:=ws.Range("B7"), Order1:=xlAscending, Header:=xlGuess
        'Copy the range
        Copyto.Copy
        Sheet23.Unprotect
        Addme.PasteSpecial xlPasteValues
    End Select
    'Reset the next row
    Set Addme = Sheet23.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
Next ws
'Sort the combined sheet
Set CombineSort = Sheet23
CombineSort.Range("cRegion").Sort Key1:=CombineSort.Range("B7"), Order1:=xlAscending, Header:=xlGuess
'Address any misaligned data or extra spaces after transfer to combined sheet
Sheet23.Activate
Range("B7:CJ3007").Select
'Weed out any formulas from selection
  If Selection.Cells.Count = 1 Then
    Set rng = Selection
  Else
    Set rng = Selection.SpecialCells(xlCellTypeConstants)
  End If
'Trim and Clean cell values
  For Each Area In rng.Areas
    Area.Value = Evaluate("IF(ROW(" & Area.Address & "),CLEAN(TRIM(" & Area.Address & ")))")
  Next Area
  Range("A1").Select
  ThisWorkbook.Save
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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