Range.Copy メソッド (Excel) | Microsoft Learn

指定された範囲をコピーする方法について説明します。Office Add-insモデルを使うと、さまざまなプラットフォームでのOffice体験を拡張できます。VSTOアドインよりも軽量で、HTML5、JavaScript、CSS3、XMLなどほとんどのウェブ技術を使用して作成可能です。expression.Copy(Destination)を使用し、指定された範囲を新しい範囲にコピーします。引数を省略すると、クリップボードにコピーされます。コード例では、特定の条件に基づき、行を異なるシートにコピーする方法を示しています。

Excelのコピー機能に関する解説

Excelでは、特定の範囲を指定した範囲にコピーすることができます。また、クリップボードにコピーすることも可能です。本記事では、Excelのコピー機能の使用方法について詳しく解説します。

興味がある方へ

Office体験を複数のプラットフォームで拡張するソリューションの開発に興味がありますか?新しいOfficeアドインモデルをチェックしてください。Officeアドインは、VSTOアドインやソリューションに比べて小さなフットプリントを持ち、HTML5、JavaScript、CSS3、XMLなどのほぼすべてのWebプログラミング技術を使用して構築できます。

構文

vba
expression.Copy(Destination)

  • expression: `Rangeオブジェクトを表す変数です。

パラメーター

名称 必要/任意 データ型 説明
Destination 任意 Variant 指定された範囲がコピーされる新しい範囲を指定します。この引数が省略されると、Microsoft Excelは範囲をクリップボードにコピーします。

戻り値

  • Variant: コピー処理の結果を返します。

以下のコード例では、Sheet1のA1:D4の数式をSheet2のE5:H8にコピーします。

vba
Worksheets(“Sheet1”).Range(“A1:D4”).Copy _
destination:=Worksheets(“Sheet2”).Range(“E5:H8”)

次のコード例では、Sheet1の各行のD列の値を調べます。D列の値が”A”であれば、その行全体を次の空の行にSheetAにコピーします。値が”B”の場合は、SheetBにコピーします。

vba
Public Sub CopyRows()
Sheets(“Sheet1”).Select
‘ データの最終行を見つける
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
‘ 各行をループ
For x = 2 To FinalRow
‘ D列に基づいてコピーするか判断
ThisValue = Cells(x, 4).Value
If ThisValue = “A” Then
Cells(x, 1).Resize(1, 33).Copy
Sheets(“SheetA”).Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets(“Sheet1”).Select
ElseIf ThisValue = “B” Then
Cells(x, 1).Resize(1, 33).Copy
Sheets(“SheetB”).Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets(“Sheet1”).Select
End If
Next x
End Sub

サポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBAサポートとフィードバックをご覧ください。サポートを受けたり、フィードバックを提供したりする方法についてのガイドラインがあります。


この機能を効果的に利用することで、Excelでの作業をよりスムーズに進めることができるでしょう。ぜひ試してみてください。

————-

Range.Copy method (Excel) | Microsoft Learn

Source link

Summary of Copying Ranges in Excel VBA

The Copy method in Excel VBA allows you to duplicate a specified range of cells, either to a new location or the clipboard.

Syntax

vba
expression.Copy(Destination)

  • expression: A variable representing a Range object.
  • Destination (Optional): A variant that specifies where to copy the range. If omitted, the range is copied to the Clipboard.

Return Value

  • The method returns a Variant.

Example Usage

  1. Copying Formulas to a New Range:
    This example copies cells A1:D4 from “Sheet1” to E5:H8 on “Sheet2”:
    vba
    Worksheets(“Sheet1”).Range(“A1:D4”).Copy _
    destination:=Worksheets(“Sheet2”).Range(“E5:H8”)

  2. Conditional Row Copying:
    This subroutine checks column D in “Sheet1”. Depending on the value, it copies the entire row to either “SheetA” or “SheetB”:
    vba
    Public Sub CopyRows()
    Sheets(“Sheet1”).Select
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To FinalRow 
        ThisValue = Cells(x, 4).Value 
        If ThisValue = "A" Then 
            Cells(x, 1).Resize(1, 33).Copy 
            Sheets("SheetA").Select 
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1 
            Cells(NextRow, 1).Select 
            ActiveSheet.Paste 
            Sheets("Sheet1").Select 
        ElseIf ThisValue = "B" Then 
            Cells(x, 1).Resize(1, 33).Copy 
            Sheets("SheetB").Select 
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1 
            Cells(NextRow, 1).Select 
            ActiveSheet.Paste 
            Sheets("Sheet1").Select 
        End If 
    Next x 

    End Sub

Office Add-ins Note

For those interested in extending Office functionalities across various platforms, the new Office Add-ins model allows for the development of lightweight solutions using web technologies like HTML5, JavaScript, and CSS3.


If you have any questions or need feedback regarding Office VBA, you can refer to the official Office VBA support resources.

関連記事