在不均匀分布的电子表格Excel VBA上合并值
编写基本的VBA来填充一个二维数组,其中一个由奇数列组成,另一个是偶数列的总和,然后总计在另一个数组中存储的可变数量的行中。 二维数组然后打印在一个单独的工作表上。 我在同一个文件中的两个其他工作表上成功地完成了这个任务的代码,这个工作表的大小略有不同,但是当调整了新的input和输出时,它用零填充目标范围。
有问题的代码:
Sub dad() Dim i As Integer, j As Integer, units As Double, value As Double, mr(1 To 655, 1 To 3) As Double, u As Integer, here As Range Dim thisone As String, there As Range thisone = Worksheets("MB52 for 1010").Cells(1, 1).Address Set here = Range(thisone) MsgBox (here(1, 1).Address) thisone = Worksheets("1010totals").Cells(1, 1).Address Set there = Range(thisone) MsgBox (there(1, 1).Address) For i = 1 To 655 mr(i, 1) = Worksheets("1010totals").Cells(i + 1, 4).value Next i MsgBox ("array made") i = 1 u = 1 MsgBox (i & " " & u) For i = 1 To 655 For j = 1 To mr(i, 1) u = u + 1 units = here(u, 6) + here(u, 9) + here(u, 11).value + here(u, 13) + here(u, 15) + here(u, 17) value = here(u, 8) + here(u, 10) + here(u, 12).value + here(u, 14) + here(u, 16) + here(u, 18) Next j mr(i, 2) = units mr(i, 3) = value Next i For i = 1 To 655 For j = 2 To 3 Worksheets("1010totals").Cells(i + 1, j).value = mr(i, j) Next j Next i End Sub
在其他工作表上工作的原始代码:
Sub ded() Dim i As Integer, j As Integer, units As Double, value As Double, n As Integer, mr(1 To 756, 1 To 3) As Double, u As Integer, here As Range Dim thisone As String, there As Range thisone = Worksheets("MB52 for 1030").Cells(1, 1).Address Set here = Range(thisone) MsgBox (here(1, 1).Address) thisone = Worksheets("1030totals").Cells(1, 1).Address Set there = Range(thisone) MsgBox (there(1, 1).Address) For i = 1 To 756 mr(i, 1) = Worksheets("1030totals").Cells(i + 1, 4).value Next i MsgBox ("array made") i = 1 u = 1 MsgBox (i & " " & u) For i = 1 To 756 For j = 1 To mr(i, 1) u = u + 1 units = here(u, 6) + here(u, 9) + here(u, 11).value + here(u, 13) + here(u, 15) + here(u, 17) value = here(u, 8) + here(u, 10) + here(u, 12).value + here(u, 14) + here(u, 16) + here(u, 18) Next j mr(i, 2) = units mr(i, 3) = value Next i For i = 1 To 756 For j = 2 To 3 Worksheets("1030totals").Cells(i + 1, j).value = mr(i, j) Next j Next i End Sub