Macro Help - Copy from Clipboard to start my Macro

Copper Contributor
Hi Guys,
 

I need your help in a macro I am trying to build. I have a procedure where I start by coping to clipboard  a row from another excel sheet, with an intention of pasting on my current sheet and edit further.

To automate my procedure, I am running the below macro.

 

code.JPG

When I am running the code, I am getting a 'run-time error '1004'"

 

error.JPG

I suspect the error occurs as I am not instructing it to copy the clipboard content.

Can you fix my code, for it starts with coping from clipboard (and the rest remain as it is?)

 

Thanks in advance,

Kimon

 

PS:

My current code is:

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("DATA1").Select
    Rows("2:2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Rows("3:3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("SCANER").Select
    Rows("1:1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
6 Replies

@kimon90 

The first copy action is not in your macro. Perhaps you should add that to the macro.

Hi Hans,
Your remark is correct. I do not know how to add the first copy action in my macro. In fact, I do not know the exact wording to instruct it to start by coping from clipboard. Can you please edit my code to add the first copy action is not in my macro?
Thanks

@kimon90 

The following version will copy whichever range you have selected when you run the macro.

 

Sub Macro1()
'
' Macro1 Macro
'
    Selection.Copy
    Sheets("DATA1").Rows("2:2").PasteSpecial Paste:=xlPasteValues
    Sheets("DATA1").Rows("3:3").Copy
    Sheets("SCANER").Rows("1:1").PasteSpecial Paste:=xlPasteValues
End Sub
Hi Hans,

If I understand correctly, I should stick your bit before my bit so when I start running the macro it copies from clipboard and continues the rest of the actions ?

Should the final code be like this?

Sub Macro1()
'
' Macro1 Macro
'

'
Selection.Copy
Sheets("DATA1").Rows("2:2").PasteSpecial Paste:=xlPasteValues
Sheets("DATA1").Rows("3:3").Copy
Sheets("SCANER").Rows("1:1").PasteSpecial Paste:=xlPasteValues

Sheets("DATA1").Select
Rows("2:2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("3:3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SCANER").Select
Rows("1:1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

Or am I wrong and the code you wrote is a better/shorter version of mine and it stands by itself ?

Thanks again,
Kimon

@kimon90 

My version is intended to completely replace yours.

In most situations, it isn't necessary to select worksheets and ranges in a macro; it runs more efficiently if you don't.

Hi Hans,

The code worked, thanks. I copy my row and then run the macro and it's great. There's only one problem. If I copy my row, then click anywhere, then run the macro, it doesn't work. I am trying to say that there should be absolutely no actions between copying and running the macro.

Can we fix that ? Should a line be added between the first and second line to ignore any Inbetween actions and go see the clipboard regardless of when the copy was made ?

Thanks in advance,
Kimon