我对使用VBA向二维数组添加值有疑问。情况如下:假设我有一个行数据。
(第一行是图块,q:测验,s:学期)
Student name q1_s1 q2_s2 q3_s1 average_s1 q1_s2 q2_s2 q3_s2 average_s2
David. 5 6 7 6. 8. 9. 10. 9
该行存储在sheet1中,现在存储在sheet 2(名为David)中,我想复制这些数据并以这种方式列出数据。
average quiz1. quiz2. quiz3
semester_1. 6. 5. 6. 7
semester_2. 9. 8 9 10
有人可以帮助我们解决这个问题吗?我是否应该使用二维数组来存储它们?
非常感谢!!
答案 0 :(得分:2)
带有数组。这将读入标头,但仅输出重新排列的数据,而不包含新的标头。如果您添加数据,则此代码可处理多于1个人的行。请注意,我已经纠正了我认为是重复输入q2_s2
的错字。一审应为q2_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
如果学生可以有不同的学期数,请将您的表格设置为最大可能的学期数,并将这些学期的空白留给特定的学生,则不进行测验,然后使用代码:
Option Explicit
Public Sub test()
Dim arr(), ws As Worksheet, i As Long, j As Long, r As Long, c As Long, outputArr(), numberOfColumns As Long
Set ws = ThisWorkbook.Worksheets("Sheet5"): arr = ws.[B1:M3].Value
numberOfColumns = UBound(arr, 2) / 4
ReDim outputArr(1 To numberOfColumns * (UBound(arr, 1) - 1), 1 To UBound(arr, 2) / numberOfColumns)
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(Ubound(arr,1) + 5 , 1).Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End Sub
示例布局,其中最大学期为3个学期,一名学生仅完成2个学期:
答案 1 :(得分:0)
如果我理解您的问题,则不需要二维数组,只需要将数据从sheet1复制到sheet2。
这是一个例子:
Public Class PropertyPolicy
Private agentfield As Entity
Private aininsuredfield() As Entity
Private billinginfofield As BillingInfo
Private cancellationdatefield As Date
Private claimsfield() As Claims
Public Property Agent As Entity
Get
Return Me.agentfield
End Get
Set(ByVal value As Entity)
Me.agentfield = value
End Set
End Property
Public Property AINInsured() As Entity()
Get
Return Me.aininsuredfield
End Get
Set(ByVal value As Entity())
Me.aininsuredfield = value
End Set
End Property
Public Property BillingInfo As BillingInfo
Get
Return Me.billinginfofield
End Get
Set(ByVal value As BillingInfo)
Me.billinginfofield = value
End Set
End Property
Public Property CancellationDate As Date
Get
Return Me.cancellationdatefield
End Get
Set(ByVal value As Date)
Me.cancellationdatefield = value
End Set
End Property
Public Property Claims() As Claims()
Get
Return Me.claimsfield
End Get
Set(ByVal value As Claims())
Me.claimsfield = value
End Set
End Property
End Class
Dim propTemp1 As New PropertyPolicy
Dim propTemp2 As New PropertyPolicy
Dim propTempComb As New PropertyPolicy
propTemp1.AINInsured = LoadEstateAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTemp2.AINInsured = LoadAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)
它已经过测试并且可以工作。 希望对您有帮助