您的问题向我表明,您希望将第一张表上 m、f、c 和 l 位置的值复制到第二张表上每个主题的单行中。以下就是这样做的(并假设您已经手动将标题复制到与第一页相同的列中)。如果我对您的问题的理解有误,请告诉我,我将尝试相应地调整代码示例。
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
在您的示例表上运行此代码后,工作表二具有以下内容:
1111 毫升
2222 毫升