Code: Select all
Sub Sandto()
Dim sourceWb As Workbook
Dim wb As Variant
Dim source As Range
Application.ScreenUpdating = False
Set sourceWb = ThisWorkbook
If Range("B5") = "" Then Exit Sub
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B5:E5")
Set wb = Workbooks.Open("D:\เส้นทางเอกสาร\เส้นทางเอกสาร.xlsx", False, False)
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B6:E6")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B7:E7")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B8:E8")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B9:E9")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B10:E10")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B11:E11")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B12:E12")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B13:E13")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B14:E14")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B15:E15")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B16:E16")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B17:E17")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B18:E18")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B19:E19")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B20:E20")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B21:E21")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B22:E22")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B23:E23")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B24:E24")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B25:E25")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B26:E26")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B27:E27")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B28:E28")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Set source = sourceWb.Sheets("ออกไปส่งลูกค้า").Range("B29:E29")
i = wb.Worksheets("เส้นทางเอกสาร").Columns("A:A").Find(source, LookIn:=xlValues).Row
source.Offset(0, 4).Copy
wb.Worksheets("เส้นทางเอกสาร").Range("E" & i).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = False
wb.Close True
Range("B3:D3,F3:G3,I3,B5:B29").ClearContents
MsgBox "เพิ่มข้อมูลเสร็จแล้ว"
End Sub
ผมพยายามเขียน Code วน Loop แล้วแต่ไม่สำเร็จ ผมเลยเขียน Code แบบบ้านๆให้พอใช้งานได้ก่อนครับ