Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Problem to paste

Hi,
I get this
User generated imagedue to this line
    ActiveSheet.Paste

Why?
Avatar of Professor J
Professor J

You are missing the range

You cannot simply paste without specifying the range where it need to paste

So you should use something like activesheet.range("a1")
Avatar of Peter Chan

ASKER

But right before running that, there is already one row that has been selected.
i dont have you full code, so cannot understand whats going on.

if you want a range to be copied from a specific sheet into the selected cell "activecel" of activesshet then use the below code

Sub tEST()
ActiveSheet.Cells(1, 1).Select
Worksheets("Sheet1").Range("A1:B4").Copy Destination:=ActiveCell

End Sub

Open in new window

Before encountering the current problem, I've already selected one row and have pressed "Copy", before I pressed one button to call the relevant codes.
In this case use Selection.Paste
I use this

    Selection.Paste

now, but I still get
User generated image
i dont have your full code.

so i will illustrate it just in a dummy example.  lets say you want to copy a range a1 to e13 from Sheet2 to Sheet1

here is the example

Sub Macro1()
    Sheets("Sheet2").Range("A1:E13").Copy
    On Error Resume Next
Sheets("Sheet1").Select
On Error GoTo 0
Range("B2").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Open in new window

Can you please refer to the attached Excel file?
I get
User generated image
once I've pressed 3rd button within 3rd sheet, inside which I've put this line

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Open in new window

t6.xlsm
you have uploaded a workbook where its vba is password protected, also the code i referred earlier was not seen in any of the VBA modules of workbook
Sorry, please refer to the attached.
t6.xlsm
Instead.
i could not spot the error myself.
can you please paste the code and indicate on which line you get the error which part of the code the yellow debugger stops?
The error has happened once I've pressed 3rd button called "Paste Copied Selection" within the Worksheet "Utilization-PO-List".
here is your code on the Worksheet "Utilization-PO-List" it is totally wrong.  where is your copy? you have to have the copy and the specific cell/range activate before the SelectionPaste Special could work.  unclear message.
by clicking the Paste Copied Select icon, from where it needs to get copied and where it needs to be pasted?  both mandatory arugments are missing in your code.

Private Sub CommandButton3_Click()
    Dim Message0 As String
    Unprotect_Workbook Message0
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Protect_Workbook Message0
End Sub

Open in new window

I expect that the user does choose a range and have pressed "Ctrl-C", right before pressing such button. Is this fine?
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do encounter this
User generated image
due to last 2nd line below

Private Sub CommandButton3_Click()
   Dim copyRange As Range, pasteRange As Range
   Dim Message0 As String
   'On Error Resume Next
   Set copyRange = Application.InputBox("Select the range to be copied.", "Copy Range Selection!", Type:=8)
   Set pasteRange = Application.InputBox("Select the destination cell where you want to paste the copied range.", "Destination Range Selection!", Type:=8)
   If copyRange Is Nothing Then
      MsgBox "You didn't select the range to be copied.", vbExclamation, "Range Not Selected!"
      Exit Sub
   End If
   
   If pasteRange Is Nothing Then
      MsgBox "You didn't select the destination cell.", vbExclamation, "Destination Not Selected!"
      Exit Sub
   End If
   
   Unprotect_Workbook Message0
   copyRange.Copy
   pasteRng.PasteSpecial xlPasteValues
   Protect_Workbook Message0

Open in new window

Why did you comment the following line? It is required there if user doesn't pick a range and click Cancel.

On Error Resume Next

Open in new window