Vba 通过多个数组循环以在excel工作簿之间传输信息

Vba 通过多个数组循环以在excel工作簿之间传输信息,vba,excel,Vba,Excel,我编写了一些代码,从另一个预格式化工作表填充另一个工作簿中的预格式化工作表。它们包括合并的细胞和所有其他令人讨厌的东西,无论出于什么原因都无法改变 因此,我写了以下内容 Sub test() Dim wbkCurrent As Workbook 'Dim wbk3Mth As Workbook Dim wbk6Mth As Workbook Set wbkCurrent = ThisWorkbook Set wbk6Mth = Workbooks.Open("C:\newbook.xlsm"

我编写了一些代码,从另一个预格式化工作表填充另一个工作簿中的预格式化工作表。它们包括合并的细胞和所有其他令人讨厌的东西,无论出于什么原因都无法改变

因此,我写了以下内容

Sub test()

Dim wbkCurrent As Workbook
'Dim wbk3Mth As Workbook
Dim wbk6Mth As Workbook

Set wbkCurrent = ThisWorkbook
Set wbk6Mth = Workbooks.Open("C:\newbook.xlsm")

newbook.Sheets("Mon 1").Activate
Call assignArrays

End Sub

Sub assignArrays

Call moveValues(32, 3, 7, 8)
Call moveValues(32, 5, 23, 6)
Call moveValues(32, 65, 15, 8)
Call moveValues(32, 56, 31, 5)
Call moveValues(32, 57, 31, 11)
Call moveValues(32, 15, 39, 4)
Call moveValues(32, 16, 39, 5)
Call moveValues(32, 17, 39, 6)
Call moveValues(32, 18, 39, 7)
Call moveValues(32, 30, 39, 10)
Call moveValues(32, 31, 39, 11)
Call moveValues(32, 32, 39, 12)
Call moveValues(32, 33, 39, 13)

Call moveValues(32, 7, 7, 21)
Call moveValues(32, 9, 23, 19)
Call moveValues(32, 66, 15, 21)
Call moveValues(32, 59, 31, 18)
Call moveValues(32, 60, 31, 24)
Call moveValues(32, 20, 39, 17)
Call moveValues(32, 21, 39, 18)
Call moveValues(32, 22, 39, 19)
Call moveValues(32, 23, 39, 20)
Call moveValues(32, 35, 39, 23)
Call moveValues(32, 36, 39, 24)
Call moveValues(32, 37, 39, 25)
Call moveValues(32, 38, 39, 26)

Call moveValues(32, 11, 7, 34)
Call moveValues(32, 13, 23, 32)
Call moveValues(32, 67, 15, 34)
Call moveValues(32, 62, 31, 31)
Call moveValues(32, 63, 31, 37)
Call moveValues(32, 25, 39, 30)
Call moveValues(32, 26, 39, 31)
Call moveValues(32, 27, 39, 32)
Call moveValues(32, 28, 39, 33)
Call moveValues(32, 40, 39, 36)
Call moveValues(32, 41, 39, 37)
Call moveValues(32, 42, 39, 38)
Call moveValues(32, 43, 39, 39)

End Sub

Sub moveValues(tRow, tCol, rRow, rCol)
'trow is row in this workbook, tcol is column in this workbook, rRow & rCol are the same for the other workbook

ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value
tRow = tRow + 1
rRow = rRow + 1
ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value
tRow = tRow + 1
rRow = rRow + 1
ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value
tRow = tRow + 1
rRow = rRow + 1
ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value
tRow = tRow + 1
rRow = rRow + 1
ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value
tRow = tRow + 1
rRow = rRow + 1
ActiveSheet.Cells(rRow, rCol).Value = ThisWorkbook.Sheets("Results").Cells(tRow, tCol).Value

End Sub
这工作正常,并将所有数据都写出来。问题是,我需要从哪里开始运行 trow=2,12,22,32,42,52 现在我可以手工把这些都写出来,但这意味着以后再修改它将是一场噩梦。因此,我想到使用
a=2,12,22,32等
,然后 调用移动值(a、3、7、8) 但是,这意味着
a
会通过moveValues子例程增加一个数字,并且每次都需要重置

我有一个使用数组解决这个问题的想法,但这有它自己的问题

我将模块
assignArrays
替换为

Sub assignArrays()

'row in this workbook
Dim array1(5)
array1(5) = Array(2, 12, 22, 32, 42, 52)

'E
Dim array2(12)
array2(12) = Array(3, 5, 65, 56, 57, 15, 16, 17, 18, 30, 31, 32, 33)

'U
Dim array2_1(12)
array2_1(12) = Array(7, 9, 66, 59, 60, 20, 21, 22, 23, 35, 36, 37, 38)

'R
Dim array2_2(12)
array2_2(12) = Array(11, 13, 67, 62, 63, 25, 26, 27, 28, 40, 41, 42, 43)

'row in report
Dim array3(12)
array3(12) = Array(7, 23, 15, 31, 31, 39, 39, 39, 39, 39, 39, 39, 39) 'constant in each array 1

'column in report
Dim array4(12)
array4(12) = Array(8, 6, 8, 5, 11, 4, 5, 6, 7, 10, 11, 12, 13) '+13 for each third

Dim v1, v2, v3, v4 As Integer

For a = 0 To 5
v1 = array1(a)
    For b = 0 To 12
        v3 = array3(b)
            For c = 0 To 12
                v4 = array4(c)
                    For d = 0 To 12
                        v2 = array2(d)
                        Call moveValues(v1, v2, v3, v4)
                    Next d
            Next c
            For c = 0 To 12
                v4 = array4(c) + 13
                    For d = 0 To 12
                        v2 = array2(d)
                        Call moveValues(v1, v2, v3, v4)
                    Next d
            Next c
            For c = 0 To 12
                v4 = array4(c) + 26
                    For d = 0 To 12
                        v2 = array2(d)
                        Call moveValues(v1, v2, v3, v4)
                    Next d
            Next c
    Next b
Next a
End Sub

这将导致MoveValue的第一行出现1004错误。有没有解决这两个问题的方法?

您没有正确处理阵列

Dim array1(5) 'Array with 5 dimension
array1(5) = Array(2, 12, 22, 32, 42, 52) 'Write all this content to the fifth position
正确的方法是:

Dim array1(5) As Integer
array1(0) = 2
array1(1) = 12
array1(2) = 22
array1(3) = 32
array1(4) = 42
array1(5) = 52
如果你想依靠一条线,你可以做:

Dim array1 
array1 = Array(2, 12, 22, 32, 42, 52) 'In this case, it starts from 0 -> pretty unconventional (bear in mind that the array above is dimensioned from 1)
----更新

您的代码提供了什么:

Dim array1(5)
array1(5) = Array(2, 12, 22, 32, 42, 52)

Dim test1 As Integer: test1 = array1(0)  '-> 0
Dim test2 As Integer: test2 = array1(1)  '-> 0
Dim test3 As Integer: test3 = array1(2)  '-> 0
Dim test4 As Integer: test4 = array1(3)  '-> 0
Dim test5 As Integer: test5 = array1(4)  '-> 0
Dim test6 As Integer: test6 = array1(5)  'ERROR
Dim array1 
array1 = Array(2, 12, 22, 32, 42, 52)

Dim test1 As Integer: test1 = array1(0)  '-> 2
Dim test2 As Integer: test2 = array1(1)  '-> 12
Dim test3 As Integer: test3 = array1(2)  '-> 22
Dim test4 As Integer: test4 = array1(3)  '-> 32
Dim test5 As Integer: test5 = array1(4)  '-> 42
Dim test6 As Integer: test6 = array1(5)  '-> 52
我的代码提供了什么:

Dim array1(5)
array1(5) = Array(2, 12, 22, 32, 42, 52)

Dim test1 As Integer: test1 = array1(0)  '-> 0
Dim test2 As Integer: test2 = array1(1)  '-> 0
Dim test3 As Integer: test3 = array1(2)  '-> 0
Dim test4 As Integer: test4 = array1(3)  '-> 0
Dim test5 As Integer: test5 = array1(4)  '-> 0
Dim test6 As Integer: test6 = array1(5)  'ERROR
Dim array1 
array1 = Array(2, 12, 22, 32, 42, 52)

Dim test1 As Integer: test1 = array1(0)  '-> 2
Dim test2 As Integer: test2 = array1(1)  '-> 12
Dim test3 As Integer: test3 = array1(2)  '-> 22
Dim test4 As Integer: test4 = array1(3)  '-> 32
Dim test5 As Integer: test5 = array1(4)  '-> 42
Dim test6 As Integer: test6 = array1(5)  '-> 52

计算出了使用
a=2,12,22等的答案
-只需使用
byVal
-仍然,为什么数组不能工作?谢谢-但我使用
的是a=0到5 v1=array1(a)
,这没有考虑到它吗?声明数组的sub工作正常,但是数组中的值没有被传输到
moveValues
sub行array1(5)=数组(2,12,22,32,42,52)理论上是错误的(对于您想要的)。我还没有测试过它,我也不完全确定它的确切性能,但是如果您遇到了错误,为什么不编写所有支持编写的代码并减少可能的问题源呢?请告诉我它崩溃的确切位置和你得到的错误。@OliverLockett我必须部分更正我的一个陈述。在与TimWilliams的讨论中,我了解到用户定义的VBA数组始终是基于零的(内置属性数组的情况并非如此,通常是基于1的)。这一事实并不会从根本上影响我给你的答案(你的数组是错的,我的数组是对的),但我提出的第一个数组声明中隐含了这个想法:它的维数超过了1。我马上换。无论如何,这对你的问题没有任何影响,我希望你的问题已经解决了。