同时更改循环变量 VBA
Simultaneously changing variables for loops VBA
正在尝试让 BRarr1 和 BRarr2 同时更改。我知道 运行s 目前它会在更改 a 之前先更改 b。
有没有办法 运行 这样的事情,以便在继续代码之前改变两张表?
例如。我需要 运行 通过 BR01 和 BR01A 然后 运行 通过 BR5 和 BR05A ...等等
Sub BranchAnalysis()
Dim s1 As Worksheet
Dim s2 As Worksheet
Dim a As Integer
Dim b As Integer
Dim BRarr1
Dim BRarr2
BRarr1 = Array("BR1", "BR5", "BR10", "BR11", "BR13", "BR18", "BR20", "BR21",
"BR22", "BR23", "BR25", "BR28", "BR29", "BR33", "BR35")
BRarr2 = Array("BR01A", "BR05A", "BR10A", "BR11A", "BR13A", "BR18A",
"BR20A", "BR21A", "BR22A", "BR23A", "BR25A", "BR28A", "BR29A",
"BR33A", "BR35A")
For a = LBound(BRarr1) To UBound(BRarr1)
For b = LBound(BRarr2) To UBound(BRarr2)
Set s1 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr1(a) & "")
Set s2 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr2(b) & "")
r1 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr1(a) & "").UsedRange.Rows.count
r2 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr2(b) & "").UsedRange.Rows.count
Dim i, j As Long
'line items count
Dim count1 As Long
count1 = 0
For j = 2 To r2
For i = 2 To r1
If s2.Cells(j, 1).Value = s1.Cells(i, 1).Value Then
count1 = count1 + 1
s2.Cells(j, 2).Value = count1
Else
End If
Next i
count1 = 0
Next j
blah blah more code
Next b
next a
(基于斯科特的评论)
BRarr1 和 BRarr2 都有 ubound
=14
所以只用一个循环:
替换:
For a = LBound(BRarr1) To UBound(BRarr1)
For b = LBound(BRarr2) To UBound(BRarr2)
类似:
For ZZ = 0 to 14
并在您使用 a
或 b
的任何地方使用 ZZ
正在尝试让 BRarr1 和 BRarr2 同时更改。我知道 运行s 目前它会在更改 a 之前先更改 b。
有没有办法 运行 这样的事情,以便在继续代码之前改变两张表?
例如。我需要 运行 通过 BR01 和 BR01A 然后 运行 通过 BR5 和 BR05A ...等等
Sub BranchAnalysis()
Dim s1 As Worksheet
Dim s2 As Worksheet
Dim a As Integer
Dim b As Integer
Dim BRarr1
Dim BRarr2
BRarr1 = Array("BR1", "BR5", "BR10", "BR11", "BR13", "BR18", "BR20", "BR21",
"BR22", "BR23", "BR25", "BR28", "BR29", "BR33", "BR35")
BRarr2 = Array("BR01A", "BR05A", "BR10A", "BR11A", "BR13A", "BR18A",
"BR20A", "BR21A", "BR22A", "BR23A", "BR25A", "BR28A", "BR29A",
"BR33A", "BR35A")
For a = LBound(BRarr1) To UBound(BRarr1)
For b = LBound(BRarr2) To UBound(BRarr2)
Set s1 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr1(a) & "")
Set s2 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr2(b) & "")
r1 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr1(a) & "").UsedRange.Rows.count
r2 = Workbooks("Primary to Network vBG2.xlsm").Worksheets("" & BRarr2(b) & "").UsedRange.Rows.count
Dim i, j As Long
'line items count
Dim count1 As Long
count1 = 0
For j = 2 To r2
For i = 2 To r1
If s2.Cells(j, 1).Value = s1.Cells(i, 1).Value Then
count1 = count1 + 1
s2.Cells(j, 2).Value = count1
Else
End If
Next i
count1 = 0
Next j
blah blah more code
Next b
next a
(基于斯科特的评论)
BRarr1 和 BRarr2 都有 ubound
=14
所以只用一个循环:
替换:
For a = LBound(BRarr1) To UBound(BRarr1)
For b = LBound(BRarr2) To UBound(BRarr2)
类似:
For ZZ = 0 to 14
并在您使用 a
或 b
ZZ