1

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.

4

1 に答える 1

1

特定の操作は、アクティブ シートでのみ実行できます。

追加

Range("ETBMARKER").Parent.Activate

エラーをスローする行の前。

余談ですが、使用しない変数を Dim すると、エラーの原因となった行は何もしません。次の行に別の範囲をコピーします。このコード サンプルに含めていない追加機能がない限り、これらの 2 行を削除できます。

于 2012-11-22T04:32:54.150 に答える