あなたの質問は、シート 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