0

次のコードを使用して、一部のデータを別のシートにコピーしようとしています。

Sub FilterButton()
    Dim SourceRange As Range, DestRange As Range
    Dim DestSheet As Worksheet, Lr As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'fill in the Source Sheet and range
    Set SourceRange = Sheets("Imported Data").Range("A1:K1")

    'Fill in the destination sheet and call the LastRow
    'function to find the last row
    Set DestSheet = Sheets("Test")
    Lr = lastRow(DestSheet)

    'With the information from the LastRow function we can
    'create a destination cell
    Set DestRange = DestSheet.Range("A" & Lr + 1)

    'Copy the source range and use PasteSpecial to paste in
    'the destination cell
    SourceRange.Copy
    DestRange.PasteSpecial _
            Paste:=xlPasteValues, _
            operation:=xlPasteSpecialOperationNone, _
            skipblanks:=False, _
            Transpose:=False
    Application.CutCopyMode = False

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

これを実行しようとすると、次のエラーが発生します: コンパイル エラー: Sub または Function が定義されていません (このエラーは lastRow を指しています)... どうすれば修正できますか?

編集:

   Sub FilterButton()
    Dim SourceRange As Range, SRange, DestRange, myMultipleRange As Range
    Dim DestSheet As Worksheet, Lr As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'fill in the Source Sheets and ranges
    Set SourceRange = Sheets("Imported Data").Range("A2:B:C")
    Set SRange = Sheets("Imported Data").Range("E2:E8")
    Set myMultipleRange = Union(SourceRange, SRange)

    'Fill in the destination sheet and find the last known cell
    Set DestSheet = Sheets("Test")

    'With the information on the new sheet
    Set DestRange = DestSheet.Range("A:B:C:E")

    'Copy the source range and use PasteSpecial to paste in
    'the destination cell
    myMultipleRange.Copy
    DestRange.PasteSpecial _
            Paste:=xlPasteValues, _
            operation:=xlPasteSpecialOperationNone, _
            skipblanks:=False, _
            Transpose:=False
    Application.CutCopyMode = False

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

これらの範囲が必要ですが、複数の選択で範囲を使用することはできません:(!

4

4 に答える 4

0

あなたの質問を見たばかりですが、少し遅れて知っています.あなたが知っているかどうかはわかりませんが、使用しているコードは、彼のリンクにあるRon de Bruinの例の正確なレプリカです.

http://www.rondebruin.nl/win/s3/win001.htm

エレガントな説明のために、さまざまな例を見てください。コードに含める必要がある関数の説明については、特に彼のセクションの下部を参照してください (「LastRow 関数」など - コード内のコメントはこの関数を参照しています...)。

私は引用します:

重要: マクロの例では、このページの最後のセクションにある関数を 1 つまたは複数使用しています。ワークブックの関数をワークブックの標準モジュールにコピーすることを忘れないでください。VBA を使い始めたばかりの場合は、このページを参照してください。インターネットで見つけたコードをどこに貼り付けるのですか

于 2014-11-26T12:31:09.873 に答える