หนูมีแนวการคิดแต่เขียนออกมาไม่เป็นค่ะ
หนูต้องการนำข้อมูลจาก sheet : summary1 มาเปรียบเทียบกับข้อมูลที่มีการ insert เข้ามาอัตโนมัติใน sheet : data template ให้ตรงตามเงื่อนไขคือ
check ข้อมูลที่ตรงกันในแนว row ก่อน คือ ที่ sheet : summary ถ้า ชื่อ product กับ capacity ตรงกับ ชื่อ family ละ capacity ของ sheet : mps template ถ้าตรงให้ทำการเชคในแนวคอลัมน์คือ
ที่คอลัมน์ FMONTH : เดือนในแนวคอลัมน์ของแถวนั้นตรงกับเดือนที่อยุ่ใน mps template หรือที่เซล (6,3) หรือไม่ ถ้าตรงให้แสดงค่าจาก sheet : summary1 ที่คอลัมน์ summary มาไว้ในใน sheet: mps template ให้ตรงตามชื่อ product,capacity,และเดือน เมื่อจบชื่อ product หนึ่งก็จะมีการ คำนวณบวกค่าของ summary ของ product นั้นไว้ที่ส่วนของ sub total เสมอ ในส่วนของ summary2 ก็นำมาเทียบกับ sheet : mps template เช่นเดียวกันค่ะ แต่ข้อมูลของ summary จะมาปรากฏอยุ่ในส่วนของ sheet: mps templat ตั้งแต่ คอลัมน์ G-J ค่ะ และจะนำข้อมูลทั้ง 2 mps มาทำการเปรียบเทียบค่าที่ต่างกันใน sheet : mps template ตั้งแต่คอลัมน์
จะสามารถแทรกสูตรการดึงเข้าไปในโค้ดข้างล่างได้อีกไหมค่ะ ช่วยแนะนำหน่อยค่ะหนูมีความรู้น้อยมากเกี่ยวกับการเขียนโค้ด VBA ค่ะ และยังไม่ค่อยเข้าใจเกี่ยวกับโค้ดข้างล่างนี้ด้วยค่ะ
(หนูลองใช้ vlookup แล้วมีความยืดหยุ่นน้อยค่ะเป็นการฟิคตำแหน่งข้อมูลตรงไปค่ะ)
Code: Select all
Option Explicit
Dim grandTotal As Double
Public Function compareprocap()
ActiveWorkbook.Sheets("MPS TEMPLATE").Activate
Dim rAll As Range, r As Range
Dim colR As Collection, item As Variant
Dim subTotal As Double, target As Range
Dim iMax As Integer, iCount As Integer
Set colR = New Collection
Dim grandTotal As Double
grandTotal = 0
With Sheets("NEWDATA")
Set rAll = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
End With
On Error Resume Next
For Each r In rAll ' loop for add
colR.Add r, r 'row number null
Next r
On Error GoTo 0 'debug error
For Each item In colR
iMax = Application.CountIf(rAll, item)
iCount = 0
subTotal = 0
For Each r In rAll
If r <> "" Then
With Sheets("MPS TEMPLATE")
Set target = .Range("a" & Range("a99999").End(xlUp).Row).Offset(1, 0) 'set target
End With
If r = item Then
Rows(target.Row).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow 'insert row
target.Offset(-1, 0) = r
target.Offset(-1, 0).Offset(0, 1) = r.Offset(0, 1) 'count colum capacity
subTotal = subTotal + r.Offset(0, 1)
iCount = iCount + 1
End If
If iCount = iMax Then
Rows(target.Row).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow 'insert row
With Sheets("MPS TEMPLATE").Range("a" & Range("a99999").End(xlUp).Row)
.Offset(1, 0) = "Sub Total"
.Offset(1, 1).Font.Color = -10477568
.Offset(1, 1).Font.Bold = True
grandTotal = grandTotal
subTotal = 0
End With
With Sheets("MPS TEMPLATE")
Range("a" & target.Row - 1 & ":n" & target.Row - 1).Interior.Color = 13434777
End With
Exit For
Else
End If
End If
Next r
grandTotal = grandTotal
Next item
With Sheets("MPS TEMPLATE").Range("a" & Rows.Count).End(xlUp)
.Offset(1, 0) = "Grand Total"
.Offset(1, 1).Font.Color = 15773696
.Offset(1, 1).Font.Bold = True
End With
With Sheets("MPS TEMPLATE")
Range("a" & target.Row & ":n" & target.Row).Interior.Color = 49407
End With
End Function
ขอบคุณค่ะ