循环遍历多个数组以在excel工作簿之间传输信息

时间:2013-07-16 10:17:43

标签: excel vba excel-vba

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

所以,我写了以下内容

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 etc然后拥有的想法     调用moveValues(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

这会在moveValues的第一行出现1004错误。有什么想法来解决这两种解决方案吗?

1 个答案:

答案 0 :(得分:0)

您没有正确处理数组。

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