Good morning,
I'm currently learning VBA coding, so I'm not sure if I'm doing something wrong. I have some problems with Paste
and PasteSpecial
method. Even when running a script from a recorded macro it is showing an error called "Runtime error '1004': Application-defined or object-defined error".
The simple and trivial recorded macro is the following.
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets("Aggressive").Select
ActiveCell.Range("A1:D15").Select
Selection.Copy
ActiveCell.Offset(18, 7).Range("A1").Select
Sheets("All Portfolios").Select
ActiveCell.Offset(2, -3).Range("A1").Select
ActiveSheet.Paste
End Sub
I'm not sure why this is happenening, and the same happens for written "ex novo" code and also for
PasteSpecial
method applied to a Range
obj.
Is there something I am missing? I also want to specify that it worked for some time (both Paste
and PasteSpecial
) then out of the blue this error happened and keeps happening.
I tried some solution i found on the internet but it still keeps happening and i don't think it's normal that a recorded macro doesn't work.
Thank you in advance!
---Edit Thank you very much @Dominique, this was in fact an error. I've already read the URL you posted when looking for a solution for my problem, but since for now my programs are very simple I didn't apply the "variables" way to write the codes yet. However i have just the same some problems with the copy and paste method, even when trying to apply the specified url method of variables instead of selection. The previously posted code was from a recorded macro so honestly idk why it produced a negative offset. However the following is a code i wrote while attending an online vba course, and I can't grasp if the problems lays again in offset method use or something else.
Sub GenRep()
Dim x As Integer
For x = 1 To Worksheets.Count - 1
Worksheets(x).Select
Range("B6").Select
Selection.CurrentRegion.Select
Range("B2000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(2, 0).Select
ActiveCell.Value = Worksheets(x).Name & "portfolio"
ActiveCell.Offset(2, 0).Select
ActiveSheet.Paste
Next x
End Sub