VBA Excel循环每个主题/记录多个观察

问题描述:

我有一个关于使用VBA将单元格从一个表格复制到另一个表格的问题。VBA Excel循环每个主题/记录多个观察

问题背景: Excel电子表格的

Sheet 1中包含标头和从多个测试对象的数测试的观察。每行是每个主题一次试验的观察结果。每个测试对象具有静态数量的观察值,即15行。我需要从指定行中选择一个或多个单独的单元格,并将它们复制到同一工作簿的工作表2中,以便每个主题对应一行。工作表2也将包含标题。作为VBA的新手,以及一般的编程/脚本编程,我都遇到了一些问题,如何做到这一点。以下是我试图操纵的数据的一个类似示例。下面的数据集包含5列。主题编号是1111和2222.Variant()的每个标题包含1个字母,例如1。 VariableOne = “一”,VariableTwo = “B” 等

主语 - VariableOne - VariableTwo - VariableThree - VariableFour

1111 - ABCD

1111 - EFGH

1111 - IJKL

1111 - MNOP

2222 - ABCD

2222 - EFGH

2222 - IJKL

2222 - MNOP

例如,片材2的第2行(由于头)将包括从受试者号1111和值“m”个VariableOne中的“f”,VariableTwo中的“f”,VariableThree中的“c”以及VariableFour中的“l”。工作表2的第3行将包含主题#2222和与主题1111相同位置中列出的值(值将不同,但它们在数据集内的位置将相同)。

我认为代码应该包括2个循环;一个用于遍历主题,另一个用于遍历实际数据。我不确定如何去做这个,所以如果有人以前做过这个,我会很感激帮助。

此外,我刚刚阅读了“绝对初学者的Microsoft Excel VBA编程”一书,这是一本很好的介绍VBA for excel的书,但它并没有帮助我为任何个人VBA /优秀的需求。有没有人知道更好的来源,比如一本书,能够更熟悉Excel的VBA脚本语言?

+0

看来你写了一个故事,我觉得很钻孔读取它。简要地告诉我你想让我们在这里? –

+0

我浏览了你提到的那本书。就我个人而言,我发现您可以通过录制宏并尝试修改它们以适应您的需要,从而更轻松地学习VBA(最初)。记录一个宏。修改它试图做你想做的。如果它不起作用,请分享代码并解释它所做的事情。通常,有人会告诉你如何解决它。如果你无法做到这一点,让你的问题听起来更有趣,有人可能会为你写。 –

+0

输入数据和预期输出可以说比写一本小说很多...抱歉,但事实...... :) – bonCodigo

你的问题告诉我,你正在寻找复制第一张在位置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 mfcl

2222 mfcl

+0

辉煌!非常感谢! – matt