我有一个问题,即使用VBA将单元格从一张纸复制到另一张。
问题背景:
Excel电子表格的Sheet1包含标题和来自多个测试主题的大量测试观察结果。每行是每个受试者一次试验的观察结果。每个测试对象具有静态观察数,即15行。我需要从指定的行中选择一个单独的单元格,并将它们复制到同一工作簿的sheet2,每个主题为1行。表2也包含标题。作为VBA的新手,以及一般的编程/脚本,我遇到了一些关于如何做到这一点的问题。下面是我试图操纵的数据的类似示例。下面的数据集包含5列。主题编号是1111和2222.Variable()的每个标题包含1个字母,例如VariableOne =“a”,VariableTwo =“b”等。
主题 - VariableOne - VariableTwo - VariableThree - VariableFour
1111 - a b c d
1111 - e f g h
1111 - 我知道
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脚本语言?
答案 0 :(得分: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
在示例表上运行此代码后,Sheet Two具有以下内容:
1111 m f c l
2222 m f c l