
เรียนอาจารย์และเพื่อนสมาชิกทุกคนครับ
อยากให้ปรับ code ให้ครับ
อธิบายดังนี้ ใน sheet1 คอลัมน์ D รายการที่แตกต่างจาก คอลัมน์ A ในแถวใด ให้ไปวางแทนที่ในแถวนั้นครับ ผมลองปรับไป 2 แบบยังไม่ได้ครับ
Code: Select all
Sub Source()
Dim raLL, r1aLL As Range
Dim r, r1 As Range
Dim rt1, rs1 As Range
With Sheets("Sheet1")
Set raLL = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
Set r1aLL = .Range("d2", .Range("d" & Rows.Count).End(xlUp))
End With
For Each r1 In r1aLL
For Each r In raLL
With Sheets("Sheet1")
If r1 <> r Then
With Sheets("Sheet1").Range("b" & .Rows.Count).End(xlUp)
r.Offset(0, 0).Value = r1.Offset(0, 0).Value
End With
End If
Exit For
End With
Next r
Next r1
Set r1aLL = Nothing
Set raLL = Nothing
End Sub
ติดตรงนี้ครับ
Code: Select all
With Sheets("Sheet1").Range("b" & .Rows.Count).End(xlUp)
r.Offset(0, 0).Value = r1.Offset(0, 0).Value
พอ Exit for ไปวางที่ ช่อง A2 แล้วไม่เลื่อนลงมาตามตำแหน่งที่ต้องการ
Code: Select all
Sub Source1()
Dim rSource As Range
Dim rTarget As Range
Dim rs As Range
Dim rt As Range
Set ws1 = Workbooks("Sheet1").Sheets("Sheet1")
With ws1
Set rTarget = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
Set rSource = .Range("D2", .Range("D" & Rows.Count).End(xlUp))
End With
For Each rt In rTarget
If Application.CountIf(rSource, rt) = 0 Then
With ws1.Range("A2", ws1.Range("A" & Rows.Count).End(xlUp))
.Offset(0, 0).Value = rt.Offset(0, 0).Value
End With
End If
Next rt
Set rSource = Nothing
Set rTarget = Nothing
End Sub
ถ้า code นี้ ติดตรงนี้ครับ
Code: Select all
With ws1.Range("A2", ws1.Range("A" & Rows.Count).End(xlUp))
วางซ้ำกันทั้งหมด หากไม่ ใส่ ก็จะวางที่ตำแหน่งเดียวไม่เลื่อนลงมาให้ตรงตามที่ต้องการครับ
ขอบคุณครับ