snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Sub CountAndSumData()
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim lastRowSource As Long
Dim lastRowDest As Long
Dim i As Long, j As Long
Dim countAA As Long, sumAA As Double
Dim col As Integer, cols As Long, pd As String
Dim sumUnit As Long, sumAmt As Double
Dim totalRowSum As Double
' กำหนดชีทที่ใช้งาน
Set wsSource = ThisWorkbook.Sheets("COMBINED_DATA_DTTM")
Set wsDest = ThisWorkbook.Sheets("DTTM")
' หาแถวสุดท้ายของข้อมูลในชีทต้นทาง
lastRowSource = wsSource.Cells(wsSource.Rows.count, "B").End(xlUp).Row
' หาแถวสุดท้ายของข้อมูลในชีทปลายทาง
lastRowDest = wsDest.Cells(wsDest.Rows.count, "B").End(xlUp).Row
cols = wsDest.Cells(3, wsDest.Columns.count).End(xlToLeft).Column
For col = wsDest.Columns("f").Column To cols - 2 Step 2
pd = VBA.Trim(VBA.Left(wsDest.Cells(2, col).Value, VBA.Len(wsDest.Cells(2, col).Value) - 1))
' ลูปผ่านแถวที่มีข้อมูลในชีทปลายทาง ตั้งแต่แถวที่ 4 ลงมา
For j = 4 To lastRowDest
' ล้างค่าตัวแปรก่อนเริ่มต้นการคำนวณ
countAA = 0: sumAA = 0
totalRowSum = 0
' ลูปผ่านแถวที่มีข้อมูลในชีทต้นทาง
For i = 2 To lastRowSource
If wsSource.Cells(i, "ab").Value = wsDest.Cells(j, 2).Value And wsSource.Cells(i, 24).Value = "Y" Then
If wsSource.Cells(i, 13).Value = pd Then
countAA = countAA + 1
sumAA = sumAA + wsSource.Cells(i, 20).Value
End If
End If
Next i
' เขียนค่าลงในชีทปลายทาง
wsDest.Cells(j, col).Value = countAA ' จำนวน AA ไปที่คอลัมน์ F
wsDest.Cells(j, col + 1).Value = sumAA ' ผลรวม AA ไปที่คอลัมน์ G
Next j
Next col
For i = 4 To lastRowDest
sumUnit = 0: sumAmt = 0
For col = wsDest.Columns("f").Column To cols - 2 Step 2
sumUnit = sumUnit + wsDest.Cells(i, col).Value
sumAmt = sumAmt + wsDest.Cells(i, col + 1).Value
Next col
wsDest.Cells(i, cols).Value = sumUnit
wsDest.Cells(i, cols - 1).Value = sumAmt
Next i
MsgBox "ดำเนินการประมวลผลเสร็จสิ้นแล้ว", vbInformation, "ดำเนินการประมวลผล..."
End Sub
Sub CountAndSumData()
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim lastRowSource As Long
Dim lastRowDest As Long
Dim i As Long, j As Long
Dim countค่าบริกร As Long, sumค่าบริกร As Double
Dim col As Integer, cols As Long, pd As String
Dim sumUnit As Long, sumAmt As Double
Dim totalRowSum As Double
' กำหนดชีทที่ใช้งาน
Set wsSource = ThisWorkbook.Sheets("COMBINED_DATA_DTTM")
Set wsDest = ThisWorkbook.Sheets("DTTM")
' หาแถวสุดท้ายของข้อมูลในชีทต้นทาง
lastRowSource = wsSource.Cells(wsSource.Rows.count, "B").End(xlUp).Row
' หาแถวสุดท้ายของข้อมูลในชีทปลายทาง
lastRowDest = wsDest.Cells(wsDest.Rows.count, "B").End(xlUp).Row
cols = wsDest.Cells(3, wsDest.Columns.count).End(xlToLeft).Column
For col = wsDest.Columns("f").Column To cols - 2 Step 2
pd = VBA.Trim(VBA.Left(wsDest.Cells(2, col).Value, VBA.Len(wsDest.Cells(2, col).Value) - 1))
' ลูปผ่านแถวที่มีข้อมูลในชีทปลายทาง ตั้งแต่แถวที่ 4 ลงมา
For j = 4 To lastRowDest
' ล้างค่าตัวแปรก่อนเริ่มต้นการคำนวณ
countค่าบริกร = 0: sumค่าบริกร = 0
totalRowSum = 0
' ลูปผ่านแถวที่มีข้อมูลในชีทต้นทาง
For i = 2 To lastRowSource
If wsSource.Cells(i, "ab").Value = wsDest.Cells(j, 2).Value And wsSource.Cells(i, 24).Value = "Y" Then
If wsSource.Cells(i, 13).Value = pd Then
countค่าบริกร = countค่าบริกร + 1
sumค่าบริกร = sumค่าบริกร + wsSource.Cells(i, 20).Value
End If
End If
Next i
' เขียนค่าลงในชีทปลายทาง
wsDest.Cells(j, col).Value = countค่าบริกร ' จำนวน AA ไปที่คอลัมน์ F
wsDest.Cells(j, col + 1).Value = sumค่าบริกร ' ผลรวม AA ไปที่คอลัมน์ G
Next j
Next col
For i = 4 To lastRowDest
sumUnit = 0: sumAmt = 0
For col = wsDest.Columns("f").Column To cols - 2 Step 2
sumUnit = sumUnit + wsDest.Cells(i, col).Value
sumAmt = sumAmt + wsDest.Cells(i, col + 1).Value
Next col
wsDest.Cells(i, cols).Value = sumUnit
wsDest.Cells(i, cols - 1).Value = sumAmt
Next i
MsgBox "ดำเนินการประมวลผลเสร็จสิ้นแล้ว", vbInformation, "ดำเนินการประมวลผล..."
End Sub
You do not have the required permissions to view the files attached to this post.