คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 3
VBA ใช้ได้ตั้งแต่ MS Excel รุ่นแรกๆ version 2002 น่าจะใช้ได้

copy code นี้วางที่ module1 แล้ว run macro ชื่อ Shelf_Allocate
Option Explicit
Sub Shelf_Allocate()
Dim i, j, k As Integer
Dim over As Integer
Dim distribute As String
Dim product As String
Dim amount As Integer
Dim accum As Integer
Dim ShelfCode As String
Dim ShelfCapacity As Integer
j = 4
For i = 4 To 28
If j > 12 Then
amount = 0
End If
ShelfCode = Cells(i, 1).Value
ShelfCapacity = Cells(i, 2).Value
product = Cells(j, 4).Value
amount = Cells(j, 5).Value
If accum > 0 Then
amount = amount - accum
Else
amount = Cells(j, 5).Value
End If
If ShelfCapacity < amount Then
accum = accum + ShelfCapacity
amount = ShelfCapacity
over = amount - ShelfCapacity
distribute = ShelfCode & " | " & ShelfCapacity & " >> " & product & " | " & amount
Else
accum = 0
distribute = ShelfCode & " | " & ShelfCapacity & " >> " & product & " | " & amount
j = j + 1
End If
Range("G" & i).Value = ShelfCode
Range("H" & i).Value = ShelfCapacity
Range("I" & i).Value = product
Range("J" & i).Value = amount
'Debug.Print (distribute)
Next i
End Sub

copy code นี้วางที่ module1 แล้ว run macro ชื่อ Shelf_Allocate
Option Explicit
Sub Shelf_Allocate()
Dim i, j, k As Integer
Dim over As Integer
Dim distribute As String
Dim product As String
Dim amount As Integer
Dim accum As Integer
Dim ShelfCode As String
Dim ShelfCapacity As Integer
j = 4
For i = 4 To 28
If j > 12 Then
amount = 0
End If
ShelfCode = Cells(i, 1).Value
ShelfCapacity = Cells(i, 2).Value
product = Cells(j, 4).Value
amount = Cells(j, 5).Value
If accum > 0 Then
amount = amount - accum
Else
amount = Cells(j, 5).Value
End If
If ShelfCapacity < amount Then
accum = accum + ShelfCapacity
amount = ShelfCapacity
over = amount - ShelfCapacity
distribute = ShelfCode & " | " & ShelfCapacity & " >> " & product & " | " & amount
Else
accum = 0
distribute = ShelfCode & " | " & ShelfCapacity & " >> " & product & " | " & amount
j = j + 1
End If
Range("G" & i).Value = ShelfCode
Range("H" & i).Value = ShelfCapacity
Range("I" & i).Value = product
Range("J" & i).Value = amount
'Debug.Print (distribute)
Next i
End Sub
แสดงความคิดเห็น
ขอสอบถามเรื่องสูตร Excel หน่อยครับ
คือ อยากขอให้ผู้ที่รู้การใช้สูตร Excel ช่วยคิดสูตร Excel ตรงคอลัมน์ D กับ คอลัมน์ E โดยแสดงค่าที่ได้ตามรูปนะครับ
ถ้าคิดสูตรได้หลายวิธีจะดีมากครับ
แต่สูตรที่คิดได้ต้องมี Functionไม่เกิน Excel เวอร์ชั่น 2002
ขอบคุณล่วงหน้าครับ
*หากให้ข้อมูลไม่ครบ บอกได้ครับ