VBA paste special... why isn't this working?

nicolehalliday

Board Regular
Joined
May 19, 2010
Messages
56
Hello,

I have been trying to figure out why my paste special isn't working for SO long, I'm close to giving up so hopefully someone else can spot the mistake! Is highlights yellow on the pastespecial line, but I don't understand why the paste location is invalid and have tried countless variations of below. Thanks for your help!

Code:
Sub deletepastdiv()
Dim x As Long
Dim y As Long
Dim ticker As Range
Dim divfreq As Long
Dim csheet As Worksheet
Set csheet = Worksheets("Sheet2")
Dim histsheet As Worksheet
Set histsheet = Worksheets("Historical Divs")
Dim count As Long
Dim exdate As Date
Dim twodaysago As Date
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
startrow = 4
endrow = histsheet.Range("D" & startrow).End(xlDown).Row
hendrow = csheet.Range("D" & startrow).End(xlDown).Row
 
For x = startrow To endrow
 
 
    twodaysago = csheet.Range("K2")
    For y = startrow To hendrow
        If csheet.Range("D" & x) < twodaysago Then
                If endrow > 65535 Then
                endrow = startrow - 1
 
                exdate = csheet.Range("D" & y)
                exdaterow = x
                csheet.Range("D" & exdaterow, "L" & exdaterow).Cut
                histsheets.Range("D:L" & endrow + 1).PasteSpecial
End If
        End If
    Next y
Next x
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This is not a valid range reference..
histsheets.Range("D:L" & endrow + 1).PasteSpecial

If endrow = 35 then that would translate to
histsheets.Range("D:L36").PasteSpecial

It's missing the begin Row#
histsheets.Range("D??:L36").PasteSpecial
 
Upvote 0
Paste Special isn't available after Cut. Try

Code:
csheet.Range("D" & exdaterow, "L" & exdaterow).Copy
histsheets.Range("D:L" & endrow + 1).PasteSpecial Paste:=xlPasteValues
csheet.Range("D" & exdaterow, "L" & exdaterow).ClearContents
 
Upvote 0
Combining both answers into 1...:cool:

Code:
csheet.Range("D" & exdaterow).Resize(1,9).Copy
histsheets.Range("D" & endrow + 1).Resize(1, 9).PasteSpecial Paste:=xlPasteValues
csheet.Range("D" & exdaterow).Resize(1, 9).ClearContents
 
Last edited:
Upvote 0
Or ...

Code:
    With csheet.Cells(exdaterow, "D").Resize(, 9)
        histsheets.Cells(endrow + 1, "D").Resize(, .Columns.Count) = .Value
        .ClearContents
    End With
 
Upvote 0
Thanks for the replies. Still having problems because as was mentioned, the paste range is invalid. I tried changing it to this, but still not working. Any suggestions? Thanks!!

Code:
                    csheet.Range("D" & exdaterow, "L" & exdaterow).Copy
                    histsheets.Range("D" & endrow + 1, "L" & endrow + 1).PasteSpecial paste:=xlPasteValues
                    csheet.Range("D" & exdaterow, "L" & exdaterow).ClearContents
 
Upvote 0
The code works for me, so long as all variables are correctly defined and given values.

Describe "not working"
Do you get an error? What error?
Does nothing happen?
Is the wrong data being copied?
Or pasted to the wrong place?

What are the values of the variables at the time of the error..
exdaterow and endrow ?
 
Upvote 0

Forum statistics

Threads
1,203,486
Messages
6,055,702
Members
444,809
Latest member
mwh85

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