【问题标题】:VBA Excel Loop Multiple Observations Per Subject/RecordVBA Excel循环每个主题/记录的多个观察结果
【发布时间】:2013-01-02 20:11:09
【问题描述】:

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

问题背景:

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

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

1111 - a b c d

1111 - e f g h

1111 - i j k l

1111 - m n o p

2222 - a b c d

2222 - e f g h

2222 - i j k l

2222 - m n o p

例如,表 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 脚本语言?

【问题讨论】:

  • 看来你写了一个故事,我读起来很无聊。简要告诉我你想让我们在这里做什么?
  • 我浏览了你提到的那本书。就个人而言,我发现您可以通过录制宏并尝试修改它们以满足您的需要来更轻松地(最初)学习 VBA。录制宏。修改它以尝试做你想做的事。如果它不起作用,分享代码,并解释它应该做什么。通常,有人会告诉你如何解决它。如果您无法做到这一点,请让您的问题听起来更有趣,并且有人可能会为您编写。
  • 输入数据和预期输出可以说比写小说要多...对不起,但事实... :)
  • 您的预期输出是什么?你说每个主题有 15 行,但你只显示 4 行。

标签: loops excel vba


【解决方案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 米 f c l

2222 米 f c l

【讨论】:

  • 太棒了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2018-04-05
相关资源
最近更新 更多