Excel VBA Copy and Paste - Paste Method of Worksheet Failed

jhedges

Board Regular
Joined
May 27, 2009
Messages
208
I have the following code, which works when first created; however, will fail it seems when the clipboard data is cleared. Could someone help figure out what is causing this run-time error 1004?

Code:
Sub AssignM1()
'
' AssignM1 Macro
    ActiveCell.FormulaR1C1 = _
        "=IF(ISERROR(VLOOKUP(PLDbase[[#This Row],[Personnel '#]],'Permanent Locker Dbase.xlsm'!MONTH1,3,FALSE)),"""",(VLOOKUP(PLDbase[[#This Row],[Personnel '#]],'Permanent Locker Dbase.xlsm'!MONTH1,3,FALSE)))"
    Range("M9").Select
    ActiveSheet.Paste
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Update: - I was able to do the following:

Code:
Sub AssignM1()
'
' AssignM1 Macro
    ActiveWorkbook.Sheets("PL dbase").Activate
    Range("AF9").Select
    ActiveCell.Copy
    Range("M9").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
End Sub

This corrects the run-time error. Does anyone know if I can get away from selecting the formula as a cell and place it in the script instead?
 
Upvote 0
Try

Code:
Sub AssignM1()
'
' AssignM1 Macro
    ActiveWorkbook.Sheets("PL dbase").Activate
    Range("AF9").Copy
    Range("M9").PasteSpecial Paste:=xlPasteFormulas
End Sub
 
Upvote 0
Try:

Code:
Sub AssignM1()
'
' AssignM1 Macro
    With ActiveWorkbook.Sheets("PL dbase")
        .Range("AF9").Copy
        .Range("M9").PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    End With
End Sub
 
Upvote 0
Andrew,

Thanks for your help - looks like we came up with similar answers at about the same time. :cool:

This corrects the run-time error. Do you know if I can get away from selecting the formula as a cell and place it in the script instead?

Thanks
Jason
 
Upvote 0
Andrew,

like in my first code example:

Code:
Sub AssignM1()
'
' AssignM1 Macro
    ActiveCell.FormulaR1C1 = _
        "=IF(ISERROR(VLOOKUP(PLDbase[[#This Row],[Personnel '#]],'Permanent Locker Dbase.xlsm'!MONTH1,3,FALSE)),"""",(VLOOKUP(PLDbase[[#This Row],[Personnel '#]],'Permanent Locker Dbase.xlsm'!MONTH1,3,FALSE)))"
    Range("M9").Select
    ActiveSheet.Paste
End Sub

I put the formula directly in the script to be pasted in. With the answers we came up with at the same time the formula I want to paste is in a cell I have hidden. I was trying to find a way to not have the formula in the worksheet in case it gets over written...
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

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