我正在尝试创建并循环遍历两个不同的数组,每个数组包含20个工作表。工作表来自两个不同的工作簿,“每月”和“每周”。
我有以下内容(经过一些建议编辑后):
Dim Monthly As Excel.Workbook
Set Monthly = Workbooks("name of monthly workbook")
Dim Weekly As Excel.Workbook
Set Weekly = Workbooks.Open("path to weekly workbook")
Dim mWshtNames As Variant
Dim mWshtNameCrnt As Variant
Dim wWshtNames As Variant
Dim wWshtNameCrnt As Variant
mWshtNames = Array(Monthly.Worksheets("Reading Monthly"), Monthly.Worksheets("Writing Monthly"), Monthly.Worksheets("Science Monthly"))
'and so on, to include 20 worksheets
wWshtNames = Array(Weekly.Worksheets("Reading Weekly"), Weekly.Worksheets("Writing Weekly"), Weekly.Worksheets("Science Weekly"))
'and so on, to include 20 worksheets
For Each mWshtNameCrnt In mWshtNames
For Each wWshtNameCrnt In wWshtNames
MsgBox "Monthly sheet is " + mWshtNameCrnt.Name
MsgBox "Weekly sheet is " + wWshtNameCrnt.Name
'the real code will loop here; I am using MsgBox to test that the loop is working.
Next wWshtNameCrnt
Next mWshtNameCrnt
代码的最终目标是从每个每周工作表中的特定单元格复制数据,并将其粘贴到相应的每月工作表中的相应单元格中;所以循环需要像一对一的关系。
当前结果(12个msgboxes):
预期结果(6个msgbox):
@Jeeped的答案具有相同的有效结果,其中立即窗口返回九个结果,其中六个是预期的。我想我需要同时激活“Next wWshtNameCrnt”和“Next mWshtNameCrnt”,但不知道如何写这个。
答案 0 :(得分:2)
所以循环需要像一对一的关系。
因此嵌套循环不会这样做。当你在外部循环中迭代每个项目时,内部循环在每次外部循环进行新迭代时迭代其所有项 ,借用你的表关系词汇表,它将是 one-多对多的关系。
如果它"一对一",那么你只需要1个循环,并且你的两个数组具有相同的大小。因此,从For
:
LBound(anyOfTheseArrays) To UBound(anyOfTheseArraysButPreferablyTheSameYouUseToGetTheLBound)
循环
Dim index As Long
For index = LBound(mWshtNames) To UBound(mWshtNames)
Debug.Print mWshtNames(index).Name, wWshtNames(index).Name
Next
对于每个数组中的3个项目,这将为您提供3行输出到立即窗格(Ctrl + G),这比MsgBox
调用更实用,至少对于调试。
答案 1 :(得分:1)
您似乎希望遍历每月工作表并为每个工作表循环遍历每周工作表。
Option Explicit
Sub bleh()
Dim Monthly As Excel.Workbook, Weekly As Excel.Workbook
Set Monthly = Workbooks("name of monthly workbook")
Set Weekly = Workbooks.Open("path to weekly workbook")
Dim m As Long, w As Long
Dim mWshtNames As Variant, wWshtNames As Variant
'define 20 monthly workSHEET names
mWshtNames = Array("Lorem", "Ipsum", "Dolor", "sit", "amet", _
"consectetur", "adipiscing", "elit", "Sed", "vel", _
"cursus", "purus", "Vivamus", "nec", "ex", _
"et", "lorem", "fringilla", "consectetur", "Fusce")
'define 20 weekly workSHEET names
wWshtNames = Array("Pellentesque", "quis", "viverra", "lorem", "ac", _
"sodales", "turpis", "Morbi", "in", "vulputate", _
"lectus", "Donec", "aliquam", "suscipit", "nunc", _
"eget", "bibendum", "augue", "interdum", "porta")
For m = LBound(mWshtNames) To UBound(mWshtNames)
With Monthly.Worksheets(mWshtNames(m))
Debug.Print .Name
For w = LBound(wWshtNames) To UBound(wWshtNames)
With Weekly.Worksheets(wWshtNames(w))
Debug.Print .Name
End With
Next w
End With
Next m
End Sub
这会遍历每个月度工作表,并将其.Name property发送到VBE的Immediate window。当月工作表处于“活动状态”时,它会遍历所有每周工作表并将其.Name发送到立即窗口。
虽然代码没有任何目的,但它从使用该名称的进程中调用名称,因此至少它会验证名称。
答案 2 :(得分:1)
完全跳过嵌套循环。工作表通过 subject 相互对应,但只是巧合地按名称。在进入循环之前,不是生成所有完整的名称(和/或工作表),而是通过组合主题和工作表的类型来创建索引并获取Worksheet
引用 内部 循环:
Dim Monthly As Excel.Workbook
Set Monthly = Workbooks("name of monthly workbook")
Dim Weekly As Excel.Workbook
Set Weekly = Workbooks.Open("path to weekly workbook")
Dim subjects() As String
subjects = Split("Reading,Writing,Science", ",")
Dim weeklySheet As Worksheet
Dim monthlySheet As Worksheet
Dim subject As String
For Each subject In subjects
Set weeklySheet = Weekly.Worksheets(subject & " Weekly")
Set monthlySheet = Montly.Worksheets(subject & " Monthly")
MsgBox "Monthly sheet is " + monthlySheet.Name
MsgBox "Weekly sheet is " + weeklySheet.Name
'"Real" code here.
Next
请注意,这也允许您使用强类型引用,例如Worksheet
和String
,而不必将所有内容声明为Variant
。