Copy/Paste Code Error

bpgolferguy

Active Member
Joined
Mar 1, 2009
Messages
469
Hello! Could someone tell me how to fix this code so it doesn't show an error when I click the button it's tied to? Thanks!

Private Sub TeeTimeDisplay_Click()
Range("AH2").Resize(.Range("CC2").Value).Copy
Range("J2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
There seems to be something missing, a worksheet reference in front of Range("CC2").Value.

Either that or there shouldn't be a . in front of it.
 
Upvote 0
Might be wrong be think you have a '.' where you shouldn't. Try:
Code:
Private Sub TeeTimeDisplay_Click()
Range("AH2").Resize(Range("CC2").Value).Copy
Range("J2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
End Sub
 
Upvote 0
I tried just what you put there Jack and it prompted me to take out the End With....not sure why it was there anyway....so I did, but still get the debug error.
 
Upvote 0
Perhaps you do need a With to go with the End With - one for a worksheet?

It could be that because there is no worksheet reference VBA is looking at the wrong worksheet for the value to Resize by.
 
Upvote 0
I just ran this code, not attached to a button, and it worked fine:
Code:
Sub KillerBees()
Range("A1").Resize(Range("D4")).Copy
Range("A4").PasteSpecial Paste:=xlPasteValues
End Sub
It's virtually identical to yours, so try:
Code:
Private Sub TeeTimeDisplay_Click()
Range("AH2").Resize(Range("CC2").Value).Copy
Range("J2").PasteSpecial Paste:=xlPasteValues
End Sub
If that doesn't work then I'm not sure...
 
Upvote 0
Nope, it's not wanting to do it. The sheet name is Sheet1 if that makes a difference to add that...but i'm not sure how to do that....
 
Upvote 0
If you want to add a sheet reference.
Code:
Private Sub TeeTimeDisplay_Click()
    With Worksheets("Sheet1")
        .Range("AH2").Resize(.Range("CC2").Value).Copy
        .Range("J2").PasteSpecial Paste:=xlPasteValues
    End With
End Sub
 
Upvote 0
No idea then, it's working fine for me with the code I tried. If you want to add Sheet1 then maybe:
Code:
Private Sub TeeTimeDisplay_Click()
With Sheets("Sheet1")
  .Range("AH2").Resize(.Range("CC2").Value).Copy
  .Range("J2").PasteSpecial Paste:=xlPasteValues
End With
End Sub
But if it previously wasn't working, don't see why above would now..
 
Upvote 0
If you want to add a sheet reference.
Code:
Private Sub TeeTimeDisplay_Click()
    With Worksheets("Sheet1")
        .Range("AH2").Resize(.Range("CC2").Value).Copy
        .Range("J2").PasteSpecial Paste:=xlPasteValues
    End With
End Sub

That worked! Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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