I am writing a macro to transfer data from an input form to a storage table.
I have the rough code worked out, and it runs. Only if i have the destination sheet selected. If i try to run it from the sheet containing the form it throws the following error:
Run-time error '1004': Select method of Range class failed
But yet if i run the macro from the destination sheet it executes flawlessly.
Here is the code:
Sub ExpFormCharge()
Dim pasteCell As Range
Range("expenseTbl").ListObject.ListRows.Add AlwaysInsert:=False
Range("ETBMARKER").Offset(-1, 0).Select
Range("ExpFormBackend").Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
The backend table is filled by another macro and this table is then copied and pasted into a new row in the expense table(the storage table). The line that is highlighted by the debug option is this one:
Range("ETBMARKER").Offset(-1, 0).Select
That refers to a marker, the total row of the storage table offset one so as to select the bottom row of the table.
This error occurs whenever a sheet other than the destination sheet is selected whether the code is executed from the vba window or from the macro selection pane.
Certain operations can only be performed on the Active sheet.
Add
before the line throwing the error.
Just an aside, but you Dim a variable that you never use, and the line causing the error doesn’t do anything — you copy a different range on the very next line. You could remove those two lines, unless there is additional functionality you haven’t included in this code sample.