Sub 迴圈範例()
Dim i&
[AC13] = 0
For i = 33 To 256 Step 4
If Cells(10, i) Like "前置*" = False Then
[AC13] = [AC13] + Cells(13, i)
End If
Next
End Sub
'AC13 =SUMPRODUCT((MOD(COLUMN(AG:IV),4)=1)*AG13:IV13)作者: 准提部林 時間: 2020-8-24 11:13
有看到隱藏欄,
Sub TEST()
Dim Arr, Brr, R&, C&, i&, j&, k%
R = Cells(Rows.Count, "d").End(xlUp).Row '最後一行
C = Cells(12, Columns.Count).End(xlToLeft).Column '最後一欄
Arr = Range(Cells(R, "AG"), Cells(10, C)) '定義資料範圍 AG10:???
ReDim Brr(1 To UBound(Arr) - 3, 1 To 3) '設空陣列
For i = 4 To UBound(Arr)
For j = 1 To UBound(Arr, 2) Step 4
If Left(Arr(1, j), 2) = "前置" Then GoTo j01
For k = 1 To 3
Brr(i - 3, k) = Brr(i - 3, k) + Arr(i, j + k - 1)
Next k
j01: Next j
Next i
[AC13].Resize(UBound(Brr), 3) = Brr
End Sub
Sub TEST2()
Dim Arr, Brr, R&, C&, i&, j&, k%
R = Cells(Rows.Count, "d").End(xlUp).Row
C = Cells(12, Columns.Count).End(xlToLeft).Column
Arr = Range([A1], Cells(R, C))
ReDim Brr(1 To UBound(Arr) - 12, 1 To 3)
For i = 13 To UBound(Arr)
For j = [AG1].Column To UBound(Arr, 2) Step 4
If Left(Arr(10, j), 2) = "前置" Then GoTo j01
For k = 1 To 3
Brr(i - 12, k) = Brr(i - 12, k) + Arr(i, j + k - 1)
Next k
j01: Next j
Next i
[AC13].Resize(UBound(Brr), 3) = Brr
End Sub
改成這樣是否較易理解?
Cells(R, C) 就是資料區最右/最下一格
Range([A1], Cells(R, C)) 從a1向下/向右的區域
前輩的迴圈比較難 ,換個角度比較看得懂
可能跟心境有關吧
您無私的把懂的傳授給我們 成就我們
Andy只懂得接受前輩的指導 增長自己
謝謝
For k = 1 To 3
For i = 4 To UBound(Arr)
For j = 1 To UBound(Arr, 2) Step 4
If Left(Arr(1, j), 2) = "前置" Then GoTo j01
Brr(i - 3, k) = Brr(i - 3, k) + Arr(i, j + k - 1)
j01: Next j
Next i
Next k作者: Andy2483 時間: 2020-8-25 20:21
Sub TEST_20200826()
Dim Arr, Brr, R&, C&, i&, j&, k%, T$
R = Cells(Rows.Count, "d").End(xlUp).Row '最後一行
C = Cells(12, Columns.Count).End(xlToLeft).Column '最後一欄
Arr = Range([A1], Cells(R, C)) '定義資料範圍--A1至整個區
'↑ Arr定義成整個區域更容易理解後面的邏輯規則!謝謝前輩!
ReDim Brr(1 To UBound(Arr) - 12, 1 To 20) '設空陣列
For i = 13 To UBound(Arr)
For j = [AG1].Column To UBound(Arr, 2) Step 4
T = Right(Split(Arr(11, j), "]")(0), 2) '取[??]中的文字
'↑ 因為特殊符號[ ]會影響執行結果!所以取裡面的字
C = InStr("/前置//卸模//架模//調產/", T) - 1 '檢測各分項要填入Brr的位置
'↑用T字串的所在的字數位置 決定T項目的總和放在Brr的位置! 真幸運能幫同事設計這份資料!謝謝!
If C = 1 Then '前置--取最大
For k = 0 To 2
If Arr(i, j + k) > Brr(i - 12, C + k) Then Brr(i - 12, C + k) = Arr(i, j + k)
Next k
ElseIf C >= 5 Then '其它項--累計
For k = 0 To 2
Brr(i - 12, C + k) = Brr(i - 12, C + k) + Arr(i, j + k) '各分項累計
Brr(i - 12, 17 + k) = Brr(i - 12, 17 + k) + Arr(i, j + k) '合計
Next k
End If
Next j
Next i
[M13].Resize(UBound(Brr), UBound(Brr, 2)) = Brr
End Sub作者: Andy2483 時間: 2022-9-19 16:46