0

我有一个关于使用 VBA 将单元格从一张纸复制到另一张纸的问题。

问题背景:

excel 电子表格的 Sheet1 包含标题和来自多个测试对象的一些测试观察结果。每行是对每个受试者一次试验的观察。每个测试对象都有静态数量的观察,即 15 行。我需要从指定的行中选择一个单独的单元格并将它们复制到同一工作簿的 sheet2 中,每个主题为 1 行。表 2 也将包含标题。作为 VBA 的新手,以及一般的编程/脚本,我在如何做到这一点上遇到了一些问题。下面是我试图操作的数据的类似示例。下面的数据集包含 5 列。主题编号为 1111 和 2222。Variable() 的每个标题包含 1 个字母,例如 VariableOne = "a"、VariableTwo = "b" 等。

主题 - 变量一 - 变量二 - 变量三 - 变量四

第1111章

第1111章

第1111章

第1111章

第2222章

第2222章

第2222章

第2222章

例如,表 2 的第 2 行(由于标题)将包括主题编号 1111 和来自 VariableOne 的值“m”、来自 VariableTwo 的“f”、来自 VariableThree 的“c”和来自 VariableFour 的“l”。表 2 的第 3 行将包含主题 #2222 和列在与主题 1111 相同位置的值(这些值将不同,但它们在数据集中的位置将相同)。

我认为代码应该包含 2 个循环;一个遍历主题,另一个遍历实际数据。我不太确定如何去做,所以如果有人以前做过,我将不胜感激。

此外,我刚刚阅读了“Microsoft Excel VBA 编程绝对初学者”一书,这是对 VBA for excel 的一个很好的介绍,但它并没有帮助我为我的个人 VBA/excel 需求制定任何解决方案。有没有人知道更好的资源,例如一本书,以更加熟悉用于 excel 的 VBA 脚本语言?

4

1 回答 1

1

您的问题向我表明,您希望将第一张表上 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 毫升

于 2013-01-03T17:45:20.247 回答