分享給各位有需要的板友參考並請給予指教
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.Intersect(Target, Range("B1")) Is Nothing Or Target.Count > 1 Then '如果更改的儲存格不是B列第三行以下的儲存格或更改的儲存格個數大於1時退出程式
Exit Sub
End If
Dim Rng, Ar, R As Range, ii As Integer, i As Integer
Ar = Worksheets("庫存總表").Range("B2").CurrentRegion.Value '資料庫
Set Rng = Range("A2", Range("A2").End(xlDown)).Resize(, 2)
For Each R In Rng.Rows
For ii = 1 To UBound(Ar, 1)
If R.Cells(2, 1) & R.Cells(2, 2) = Ar(ii, 2) & Ar(ii, 3) Then '項目1與項目2和資料庫項目2與項目3一樣
R.Cells(2, 7) = Ar(ii, 6) '項目7填入資料庫項目6內容
R.Cells(2, 9) = Ar(ii, 9) '項目9填入資料庫項目9內容
R.Cells(2, 6) = R.Cells(2, 5) * Range("B1") '項目6(生產用量)填入項目5(基本用量)*儲存格B1的值
R.Cells(2, 8) = R.Cells(2, 7) - R.Cells(2, 6) '項目8(剩餘數量)=庫存量-生產用量
If R.Cells(2, 8) < R.Cells(2, 9) Then '把剩餘數量跟最低庫存數進行比對,若是小於