
เรียนอาจารย์และเพื่อนสมาชิกทุกท่านครับ
มีรายการที่ยังแก้ไขไม่ได้จากการเขียน code โดยใช้ Application.CountIfs ในการหารายการที่ไม่ซ้ำกันใน 2 คอลัมน์
ซึ่งสามารถทำให้แสดงรายการออกมาได้แล้ว แต่ยังไม่ครบที่ต้องการ ขออธิบายดังนี้
จาก Sheets1 คอลัมน์ A , B ได้นำไปแสดงไว้ใน sheet2 คอลัมน์ C , B
แล้ว แต่ความต้องการให้เหมือนตามคอลัมน์ G , H รายการที่ไม่ครบคือที่ระบายสีไว้ ยกตัวอย่างคือ ในคอลัมน์ G ที่ช่อง G1 และ G2 เหมือนกันก็จริง แต่ในคอลัมน์ H ที่ ช่อง H1 และ H2 ไม่เหมือน แสดงว่ารายการในช่อง G2 และ H2 เป็นคนละรายการกันกับช่อง G1 และ H1 จึงนำมาแสดงรายการตามที่ระบายสีไว้ เป็นต้น
ช่วยปรับในส่วนนี้
Code: Select all
Application.CountIfs(rAll, r, rAll, r.Offset(0, 1)) = 0 And Application.CountIf(rTarget, r) = 0
ตัวอย่างครับ
Code: Select all
Sub ListDup()
Dim rAll As Range
Dim r As Range
Dim rTarget As Range
With Sheets("Sheet1")
Set rAll = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
With Sheets("Sheet2")
Set rTarget = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
End With
With Sheets("Sheet1")
For Each r In rAll
If Application.CountIfs(rAll, r, rAll, r.Offset(0, 1)) = 0 And Application.CountIf(rTarget, r) = 0 Then
With Sheets("Sheet2").Range("c" & .Rows.Count).End(xlUp)
Range("c" & Rows.Count).End(xlUp).Offset(1, 0) = r
Set rTarget = Range("c2", Range("c" & Rows.Count).End(xlUp))
Range("d" & Rows.Count).End(xlUp).Offset(1, 0) = r.Offset(0, 1)
End With
End If
Next r
End With
End Sub
List.xlsm
You do not have the required permissions to view the files attached to this post.