Excel 二维阵列

Excel 二维阵列,excel,vba,Excel,Vba,我有一个关于使用VBA向二维数组添加值的问题。情况如下:假设我有一行数据 (第一行是tile,q:测验,s:学期) 该行存储在sheet1中,现在存储在Sheet2中(名为David),我想复制这些数据并以这种方式列出数据 average quiz1. quiz2. quiz3 semester_1. 6. 5. 6. 7 semester_2. 9. 8 9 1

我有一个关于使用VBA向二维数组添加值的问题。情况如下:假设我有一行数据

(第一行是tile,q:测验,s:学期)

该行存储在sheet1中,现在存储在Sheet2中(名为David),我想复制这些数据并以这种方式列出数据

             average   quiz1.   quiz2.    quiz3


semester_1.    6.        5.       6.        7

semester_2.    9.        8       9      10
谁能帮我们解决这个问题?我是否应该使用二维数组来存储它们


非常感谢

如果我理解了你的问题,你不需要二维数组,只需要将数据从sheet1复制到sheet2

这是一个例子:

Sub test()
'copy data from sheet1 into sheet2(named David)
'in this code i know where are the data into sheet1 and where i want to put into sheet2(named David).

'execute macro into sheet1
Dim studentName As String

studentName = Cells(2, 1) 'in this example is David

With Sheets(studentName) 'call sheet David

    'semestre1
    .Cells(2, 3) = Cells(2, 2) 'put into cells(2,3)sheet David the data of the sheet1 is q1_s1 in semestre1 quiz1
    .Cells(2, 4) = Cells(2, 3) 'q2_s1 into semestre1 quiz2
    .Cells(2, 5) = Cells(2, 4) 'q3_s1 into semestre1 quiz3
    .Cells(2, 2) = Cells(2, 5) 'average semestre1

    'semestre2
    .Cells(3, 3) = Cells(2, 6) 'put into cells(2,3)sheet David the data of the sheet1 is q2_s1 in semestre2 quiz1
    .Cells(3, 4) = Cells(2, 7) 'q2_s2 into semestre2 quiz2
    .Cells(3, 5) = Cells(2, 8) 'q3_s2 into semestre2 quiz3
    .Cells(3, 2) = Cells(2, 9) 'average semestre2

End With
End Sub
表1数据在哪里

表2(David)信息放在哪里

它已经过测试,可以工作了。
希望这对您有所帮助

如果我理解了您的问题,您不需要二维数组,而是需要将数据从sheet1复制到sheet2

这是一个例子:

Sub test()
'copy data from sheet1 into sheet2(named David)
'in this code i know where are the data into sheet1 and where i want to put into sheet2(named David).

'execute macro into sheet1
Dim studentName As String

studentName = Cells(2, 1) 'in this example is David

With Sheets(studentName) 'call sheet David

    'semestre1
    .Cells(2, 3) = Cells(2, 2) 'put into cells(2,3)sheet David the data of the sheet1 is q1_s1 in semestre1 quiz1
    .Cells(2, 4) = Cells(2, 3) 'q2_s1 into semestre1 quiz2
    .Cells(2, 5) = Cells(2, 4) 'q3_s1 into semestre1 quiz3
    .Cells(2, 2) = Cells(2, 5) 'average semestre1

    'semestre2
    .Cells(3, 3) = Cells(2, 6) 'put into cells(2,3)sheet David the data of the sheet1 is q2_s1 in semestre2 quiz1
    .Cells(3, 4) = Cells(2, 7) 'q2_s2 into semestre2 quiz2
    .Cells(3, 5) = Cells(2, 8) 'q3_s2 into semestre2 quiz3
    .Cells(3, 2) = Cells(2, 9) 'average semestre2

End With
End Sub
表1数据在哪里

表2(David)信息放在哪里

它已经过测试,可以工作了。
希望这能帮助您使用阵列。这将读取标头,但只输出重新排列的数据,而不输出新标头。写入此命令是为了在添加数据时处理多个人行。注意:我已经纠正了我假设的打字错误,您重复了
q2\u s2
。第一个实例应该是
q2\u s1

Option Explicit
Public Sub test()
    Dim arr(), ws As Worksheet, i As Long, j As Long, r As Long, c As Long, outputArr()
    Set ws = ThisWorkbook.Worksheets("Sheet5"): arr = ws.[B1:I2].Value '<=adjust if more rows
    ReDim outputArr(1 To 2 * (UBound(arr, 1) - 1), 1 To UBound(arr, 2) / 2)
    For i = 2 To UBound(arr, 1)
        For j = LBound(arr, 2) To UBound(arr, 2) Step 4
            r = r + 1
            outputArr(r, 1) = arr(i, j + 3)
            outputArr(r, 2) = arr(i, j)
            outputArr(r, 3) = arr(i, j + 1)
            outputArr(r, 4) = arr(i, j + 2)
        Next
    Next
    ws.Cells(5, 1).Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End Sub
最大学期数为3个且1名学生仅完成2个学期的布局示例:


使用阵列。这将读取标头,但只输出重新排列的数据,而不输出新标头。写入此命令是为了在添加数据时处理多个人行。注意:我已经纠正了我假设的打字错误,您重复了
q2\u s2
。第一个实例应该是
q2\u s1

Option Explicit
Public Sub test()
    Dim arr(), ws As Worksheet, i As Long, j As Long, r As Long, c As Long, outputArr()
    Set ws = ThisWorkbook.Worksheets("Sheet5"): arr = ws.[B1:I2].Value '<=adjust if more rows
    ReDim outputArr(1 To 2 * (UBound(arr, 1) - 1), 1 To UBound(arr, 2) / 2)
    For i = 2 To UBound(arr, 1)
        For j = LBound(arr, 2) To UBound(arr, 2) Step 4
            r = r + 1
            outputArr(r, 1) = arr(i, j + 3)
            outputArr(r, 2) = arr(i, j)
            outputArr(r, 3) = arr(i, j + 1)
            outputArr(r, 4) = arr(i, j + 2)
        Next
    Next
    ws.Cells(5, 1).Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End Sub
最大学期数为3个且1名学生仅完成2个学期的布局示例:



您可以稍微不同地组织它,然后只使用数据透视表而不必担心VBA。如果您确实使用VBA,您应该尝试一下并展示您的。对此有任何进一步的更新吗?您可以稍微不同地组织它,然后只使用数据透视表而不必担心VBA。如果您确实使用VBA,您应该尝试一下显示您的。这方面有进一步的更新吗?谢谢!!!!也许我没有把我的问题弄清楚。我关心的是如果不同的学生有不同的学期和测验时间会怎样。我以前可能不知道关于学生的信息。这是一个动态的环境。我该怎么办?谢谢。谢谢!!!!也许我没有把我的问题弄清楚。我关心的是如果不同的学生有不同的学期和测验时间会怎样。我以前可能不知道关于学生的信息。这是一个动态的环境。我该怎么办?谢谢。这是基于职位的,所以如果你有不同的学期数据顺序,就不会像预期的那样工作。虽然可以适应。如果我不知道学生有多少学期呢?假设不同的学生有不同的学期。我该怎么做?取决于数据的结构。你应该保留你展示的结构,如果学生一个学期没有数据,请留空。如果继续向右添加,则需要继续添加包含4列的块,并更改UBound(arr,2)/2<2将是已知顺序的(列数)/4(不包括名称列)。从第一学期开始,然后第二学期,未知的是学期数。有些学生可能有5个学期,有些可能有3个学期。谢谢。好的。。。然后需要不同的逻辑。我必须想一想。这是基于职位的,所以如果你有不同的学期数据顺序,就不会像预期的那样工作。虽然可以适应。如果我不知道学生有多少学期呢?假设不同的学生有不同的学期。我该怎么做?取决于数据的结构。你应该保留你展示的结构,如果学生一个学期没有数据,请留空。如果继续向右添加,则需要继续添加包含4列的块,并更改UBound(arr,2)/2<2将是已知顺序的(列数)/4(不包括名称列)。从第一学期开始,然后第二学期,未知的是学期数。有些学生可能有5个学期,有些可能有3个学期。谢谢。好的。。。然后需要不同的逻辑。我得想一想。