下記のようなエクセルVBAを作成しています。for next文について営業日が多くなればなるほど、処理が重くなるのでうまく纏めて高速化を図りたいと考えています。うまくまとめる方法をご教授いただけませんでしょうか?
vba
1 2 '変数をセット 3 Dim a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19 As Long 4 5 '変数に値を入力 6 a0 = ActiveCell.Offset(0, -2).Value '当日の値 7 Dim workday As Long 8 Dim code As Long 9 workday = ActiveCell.Offset(0, -11).Value 10 code = ActiveCell.Offset(0, -9).Value 11 12 Dim i As Long, B As Variant 13 B = Range("A1:J295680") 14 For i = 1 To 295680 15 If B(i, 1) = workday - 1 And B(i, 3) = code Then a1 = B(i, 10) '1営業日前の値 16 If B(i, 1) = workday - 2 And B(i, 3) = code Then a2 = B(i, 10) '2営業日前の値 17 If B(i, 1) = workday - 3 And B(i, 3) = code Then a3 = B(i, 10) '3営業日前の値 18 If B(i, 1) = workday - 4 And B(i, 3) = code Then a4 = B(i, 10) '4営業日前の値 19 If B(i, 1) = workday - 5 And B(i, 3) = code Then a5 = B(i, 10) '5営業日前の値 20 If B(i, 1) = workday - 6 And B(i, 3) = code Then a6 = B(i, 10) '6営業日前の値 21 If B(i, 1) = workday - 7 And B(i, 3) = code Then a7 = B(i, 10) '7営業日前の値 22 If B(i, 1) = workday - 8 And B(i, 3) = code Then a8 = B(i, 10) '8営業日前の値 23 If B(i, 1) = workday - 9 And B(i, 3) = code Then a9 = B(i, 10) '9営業日前の値 24 If B(i, 1) = workday - 10 And B(i, 3) = code Then a10 = B(i, 10) '10営業日前の値 25 If B(i, 1) = workday - 11 And B(i, 3) = code Then a11 = B(i, 10) '11営業日前の値 26 If B(i, 1) = workday - 12 And B(i, 3) = code Then a12 = B(i, 10) '12営業日前の値 27 If B(i, 1) = workday - 13 And B(i, 3) = code Then a13 = B(i, 10) '13営業日前の値 28 If B(i, 1) = workday - 14 And B(i, 3) = code Then a14 = B(i, 10) '14営業日前の値 29 If B(i, 1) = workday - 15 And B(i, 3) = code Then a15 = B(i, 10) '15営業日前の値 30 If B(i, 1) = workday - 16 And B(i, 3) = code Then a16 = B(i, 10) '16営業日前の値 31 If B(i, 1) = workday - 17 And B(i, 3) = code Then a17 = B(i, 10) '17営業日前の値 32 If B(i, 1) = workday - 18 And B(i, 3) = code Then a18 = B(i, 10) '18営業日前の値 33 If B(i, 1) = workday - 19 And B(i, 3) = code Then a19 = B(i, 10) '19営業日前の値 34 35 Next i 36 ActiveCell.Offset(0, 2) = Application.WorksheetFunction.Average(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) 37
回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/29 08:52