เรียน อาจารย์ทุกท่าน
ความต้องการ
1.คีย์ข้อมูลแล้วคัดลอกข้อมูลจากชีทที่1 ไปวางในชีทที่2
2.การนำข้อมูลไปวางในชีทที่ 2 วาง ตามลำดับเลขที่(ไม่จำเป็นต้องบันทึกเรียงลำดับ)
3.ในกรณีแก้ไขข้อมูล ให้นำข้อมูลที่แก้ไขวางทับข้อมูลเดิม
Code: Select all
Sub Paste()
Dim irRange As Range
Worksheets("sheet1").Range("c3:c7").Copy
Worksheets("sheet2").Select
Set Item = Worksheets("sheet2").Range("B3:B1000").Cells.Find(What:=Range("b2").Value)
If Item Is Nothing Then
Worksheets("sheet2").Cells(65536, "B").End(xlUp).Offset(1, 0).Select
Else
Item.Activate
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
With Worksheets("sheet2")
Set irRange = .Range("b3", .Range("f" & Rows.Count).End(xlUp))
irRange.Borders.LineStyle = xlContinuous
irRange.Sort Key1:=.Range("b3"), Order1:=xlAscending, Header:=xlGuess
End With
With Worksheets("sheet1")
.Range("c3:c7").ClearContents
.Range("C4:C7").Formula = "= vlookup($C$3,range(names)" & Range("b1000").End(xlUp).Row & ",match(c3,names,0),0)"
Range("B4:c4").Copy
Range("B4").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
Worksheets("sheet1").Select
Range("C4").Select
End Sub
Code มีผลดังนี้
1. วางข้อมูลได้ตามต้องการ
2. เมื่อแก้ไขข้อมูล ข้อมูลจะเพิ่มอีก 1 row ไม่ทับข้อมูลเดิม
3. การวางสูตร ERROR ตามภาพ
4.png
สำหรับไฟล์ตัวอย่างได้แนบมาด้วยแล้ว ขอความอนุเคราะห์ด้วย
You do not have the required permissions to view the files attached to this post.