0

VBA を使用してセルをあるシートから別のシートにコピーすることに関して質問があります。

質問の背景:

Excel スプレッドシートのシート 1 には、ヘッダーと、複数の被験者からの多数のテスト結果が含まれています。各行は、被験者ごとに 1 つの試行の観察です。各被験者には静的な数の観測値、つまり 15 行があります。指定した行から個々のセルを選択し、それらを同じワークブックのシート 2 にサブジェクトごとに 1 行にコピーする必要があります。シート 2 にもヘッダーが含まれます。VBA、およびプログラミング/スクリプト全般に不慣れなため、これを行う方法について頭を抱えている問題がいくつかあります。以下は、私が操作しようとしているデータの同様の例です。以下のデータセットには 5 つの列が含まれています。件名番号は 1111 と 2222 です。Variable() の各見出しには 1 文字が含まれます。たとえば、VariableOne = "a"、VariableTwo = "b" などです。

サブジェクト - 変数 1 - 変数 2 - 変数 3 - 変数 4

1111 - abcd

1111 - エフグ

1111 - イクル

1111 - ムノップ

2222 - アブCD

2222 - エフグ

2222 - アイクル

2222 - ムノップ

たとえば、シート 2 の行 2 (ヘッダーのため) には、サブジェクト番号 1111 と、VariableOne の値「m」、VariableTwo の「f」、VariableThree の「c」、VariableFour の「l」が含まれます。シート 2 の行 3 には、サブジェクト #2222 と、サブジェクト 1111 と同じ位置にリストされた値が含まれます (値は異なりますが、データ セット内の位置は同じになります)。

コードには 2 つのループを含める必要があると思います。1 つはサブジェクトを反復処理し、もう 1 つは実際のデータを反復処理します。これを行う方法についてはよくわからないので、以前にこれを行ったことがある人がいれば、助けていただければ幸いです。

さらに、「まったくの初心者のための Microsoft Excel VBA プログラミング」という本を読んだところです。これは、VBA for Excel の優れた入門書でしたが、個人的な VBA/Excel のニーズに対するソリューションを策定するのには役立ちませんでした。Excel 用の VBA スクリプト言語に精通するために、本などのより良い情報源を知っている人はいますか?

4

1 に答える 1

1

あなたの質問は、シート 1 の位置 m、f、c、および l の値を、シート 2 の被験者ごとに 1 つの行にコピーしようとしていることを示唆しています。以下はまさにそれを行います (そして、ヘッダーをシート 1 と同じ列に手動でコピーしたと仮定します)。あなたの質問に対する私の理解が間違っている場合はお知らせください。それに応じてコード サンプルを調整します。

Sub CopyMySubjectsVariables()

    'I find making worksheet variables helps make code easier to understand
    Dim sheetOne, sheetTwo As Worksheet
    Set sheetOne = Worksheets("Sheet1")
    Set sheetTwo = Worksheets("Sheet2")

    'For the same reason, I set the column numbers to variables when possible
    Dim subjectCol, variableOneCol, variableTwoCol, variableThreeCol, variableFourCol As Integer
    subjectCol = 1
    variableOneCol = 2
    variableTwoCol = 3
    variableThreeCol = 4
    variableFourCol = 5

    'In your example table there are only four observations per subject 
    Dim numObservationsPerSubject As Integer
    numObservationsPerSubject = 4

    'Since Sheet Two also contains headers, the first subject will start on Row 2
    Dim subjectRowOnSheetTwo As Integer
    subjectRowOnSheetTwo = 2

    'Loop through all used rows of sheet one "stepping" from one subject to the next
    Dim subjectStartingRowOnSheetOne As Integer 'This variable is usually called "i" but I wanted to clarify it a bit
    For subjectStartingRowOnSheetOne = 2 To sheetOne.UsedRange.Rows.Count Step numObservationsPerSubject

        'Copy Subject Number/Name/Whatever From Sheet One to Sheet Two
        sheetTwo.Cells(subjectRowOnSheetTwo, subjectCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne, subjectCol).Value

        'Copy Variable One From the Fourth Row (startingRow+3) of the Subjects Observations ("m"'s position in your example)
        sheetTwo.Cells(subjectRowOnSheetTwo, variableOneCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 3, variableOneCol).Value

        'Copy Variable Two From Second Row (startingRow+1) of Subjects Observations ("f"'s position in your example)
        sheetTwo.Cells(subjectRowOnSheetTwo, variableTwoCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 1, variableTwoCol).Value

        'Copy Variable Three From First Row (startingRow) of Subjects Observations ("c"'s position in your example)
        sheetTwo.Cells(subjectRowOnSheetTwo, variableThreeCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne, variableThreeCol).Value

        'Copy Variable Three From Third Row (startingRow+2) of Subjects Observations ("l"'s position in your example)
        sheetTwo.Cells(subjectRowOnSheetTwo, variableFourCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 2, variableFourCol).Value

        'Increment the Starting Row on Sheet Two so the next subject starts on a new Row
        subjectRowOnSheetTwo = subjectRowOnSheetTwo + 1

    Next subjectStartingRowOnSheetOne

End Sub

サンプル テーブルでこのコードを実行すると、シート 2 には次のようになります。

1111mfcl

2222mfcl

于 2013-01-03T17:45:20.247 に答える