[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『VBAのプログラムの直し方を教えて』(大榎貴之)
こんにちは。
下記のプログラムをVBAで実行すると、オーバーフローというエラーが起きます。直し方を教えていただきたいです。エラーが起こるのは下から9行目の文です。
Sub Fixed_Quantity_Ordering_System()
'変数の定義
Dim ek, Q, k, L, ss, I, d, ad, sd, OC, SC, AII, ASQ As Integer
Dim FOQSDATA As String 'セルを定義する名前
'ユーザーフォームの表示
'UserForm_FOQS.Show
'ek = UserForm_FOQS.Period.Value '実行期間設定
'Q = UserForm_FOQS.FOQS.Value '発注量設定
ek = Cells(1, 5).Value
Q = Cells(2, 5).Value
'初期値設定
OC = 0
SC = 0
AII = 0
ASQ = 0
sd = Cells(10, 4).Value
ad = Cells(11, 4).Value
L = Cells(12, 4).Value
I = Cells(14, 4).Value
ss = Cells(21, 4).Value
'リードタイム分の表作成
k = 2
Do Until k = L + 2
Cells(k, 7).Value = k '期(k) Cells(k, 8).Value = 0 '期首の手持ち在庫量(qb(k)) Cells(k, 9).Value = 0 '需要量(d(k)) Cells(k, 10).Value = 0 '期末の手持ち在庫量(qe(k)) Cells(k, 11).Value = 0 '発注量(pr(k)) Cells(k, 12).Value = 0 '入庫量(r(K)) Cells(k, 13).Value = 0 '発注残(qr(k)) k = k + 1 Loop '初期在庫量設定 Cells(L + 1, 10).Value = I
'実行期間終了前までの数値計算
k = L + 2
Do Until k = ek + L + 2
Cells(k, 7).Value = k - L - 1 '期(k) Cells(k, 8).Value = Cells(k - 1, 10).Value '期首の手持ち在庫量(qb(k)) '需要生成 Randomize '乱数発生ルーチンを初期化 d = Application.WorksheetFunction.NormInv(Rnd(), ad, sd) If Int(d) > 0 Then Cells(k, 9).Value = Int(d) '需要(d(k)=d(k)) Else Cells(k, 9).Value = 0 '需要(d(k)=0) End If Cells(k, 10).Value = Cells(k, 8).Value - Cells(k, 9).Value '期末の手持ち在庫量(qe(k))
'在庫量が発注点以下の場合(qe(k)+qr(k-1)<ss)
If Cells(k, 10).Value + Cells(k - 1, 13).Value < ss Then Cells(k, 11).Value = Q '発注量(pr(k) =Q) Else Cells(k, 11).Value = 0 '発注量(pr(k) =0) End If Cells(k, 12).Value = Cells(k - L, 11).Value '期末の入庫量(r(k)=pr(k-L)) Cells(k, 13).Value = Cells(k - 1, 13).Value + _ Cells(k, 11).Value - Cells(k, 12).Value '発注残(qr(k)=qr(k-1)+pr(k)-r(k)) Cells(k, 10).Value = Cells(k, 10).Value + Cells(k, 12).Value '期末の手持ち在庫量に入庫量を加える(qe(k)=qe(k)+r(k))
'期末平均在庫量の計算(A1)
If Cells(k, 10).Value > 0 Then Cells(k, 14).Value = _ ((Cells(k, 8).Value + Cells(k, 10).Value) / 2) Else If Cells(k, 8).Value > 0 Then Cells(k, 14).Value = (Cells(k, 8).Value * _ Cells(k, 8).Value) / (2 * Cells(k, 9)) Else Cells(k, 14).Value = 0 End If End If AII = AII + Cells(k, 14).Value
'期平均品切れ量の計算(A2)
If Cells(k, 10).Value > 0 Then Cells(k, 15).Value = 0 Else If Cells(k, 8).Value > 0 Then Cells(k, 15).Value = (Cells(k, 10).Value * _ Cells(k, 10).Value) / (2 * Cells(k, 9)) Else Cells(k, 15).Value = -((Cells(k, 8).Value + _ Cells(k, 10).Value) / 2) End If End If ASQ = ASQ + Cells(k, 15).Value
'期発注回数の計算(A3)
If Cells(k, 11).Value > 0 Then Cells(k, 16).Value = 1 OC = OC + 1 Else Cells(k, 16).Value = 0 End If
'期品切れ回数の計算(A4)
If Cells(k, 10).Value > 0 Then Cells(k, 17).Value = 0 Else Cells(k, 17).Value = 1 SC = SC + 1 End If k = k + 1 Loop
'最初に生成した表の削除
k = 1
Do Until k = L + 1
ActiveSheet.Range("G2:R2").Delete (xlShiftUp) k = k + 1 Loop '結果の書き出し Cells(31, 3).Value = AII / ek '平均在庫量←ここでエラーが起こる Cells(32, 3).Value = ASQ / ek '平均品切れ量 Cells(33, 3).Value = OC '発注回数 Cells(34, 3).Value = SC '品切れ回数 '結果のセルへの名前定義 Worksheets("Sheet1").Range(Worksheets("Sheet1"). _ Range("G2"), Worksheets("Sheet1").Range("G2"). _ End(xlToRight).End(xlDown)).Name = "FOQSDATA" End Sub
< 使用 Excel:Microsoft365、使用 OS:Windows11 >
(リンクの冒険) 2023/06/05(月) 17:14:25
> ek = Cells(1, 5).Value > Cells(31, 3).Value = AII / ek '平均在庫量←ここでエラーが起こる > Cells(32, 3).Value = ASQ / ek '平均品切れ量
ek の内容がゼロだったりすると「実行時エラー11:0で除算しました。」になりそうですけど、 もし、AII の内容も同じくゼロだったりすると「実行時エラー6:オーバーフローしました。」になるかなぁ...
って思いました。そのへん如何でしょう?
(白茶) 2023/06/05(月) 17:18:24
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.