Page 1 of 1
Code copy data for Report
Posted: Sat Jan 11, 2014 8:25 pm
by yukioh
เรียนอาจารย์ และ ท่านผู้รู้ทุกท่าน
ขออนุญาตรบกวนสอบถาม VBA ดึง ข้อมูร จาก sheet "entry" มาใส่ sheet "report"
แต่ว่า code ที่ได้มา จะ copy แต่ข้อมุูนเพียง 2 ตัวครับ ดังไฟล์ที่แนบมา
จึงรบกวนอาจารย์ ณ ที่นี้ ขอขอบพระคุณล่วงหน้ามากครับ
Re: Code copy data for Report
Posted: Sat Jan 11, 2014 8:55 pm
by snasui

ตัวอย่าง Code ตามด้านล่าง ลองปรับใช้ดูครับ
Code: Select all
Sub Test00()
Dim rSource As Range
Dim rTarget As Range
With Sheets("Entry")
Set rSource = .Range("a10", .Range("a163").End(xlUp))
End With
With Sheets("Report")
Set rTarget = .Range("b" & .Rows.Count).End(xlUp).Offset(1, 0)
End With
rSource.Copy
rTarget.PasteSpecial xlPasteValues
rSource.Offset(0, 5).Copy
rTarget.Offset(0, 1).PasteSpecial xlPasteValues
rSource.Offset(0, 2).Copy
rTarget.Offset(0, 2).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Re: Code copy data for Report
Posted: Sat Jan 11, 2014 10:59 pm
by yukioh
ผมลองแล้วครับ ใช้ได้
ถ้าสมมุติว่า หากเรา enter ข้อมูนที่มากกว่า 163 items จะทำยังไงให้ ครับให้ flexible?
Re: Code copy data for Report
Posted: Sat Jan 11, 2014 11:03 pm
by snasui

ลองปรับมาเองก่อน ติดแล้วค่อยถามกันต่อครับ
Re: Code copy data for Report
Posted: Sat Jan 11, 2014 11:16 pm
by yukioh
ผมใส่อย่างนี้ครับ แต่ data ไม่มา ครับ
Code: Select all
Dim rSource As Range
Dim rTarget As Range
Dim LRs As Long 'last row
Dim FR As Long 'first row
With Sheets("Entry")
'Set rSource = .Range("a10", .Range("a163").End(xlUp))
LRs = .Cells.Find("Total", , , , xlByRows, xlPrevious).Row - 1
FR = .Cells.Find(shTOM, , , , xlByRows, xlPrevious).Row + 1
Set rSource = .Cells(LRs, FR).Offset(0)
End With
With Sheets("Report")
Set rTarget = .Range("b" & .Rows.Count).End(xlUp).Offset(1, 0)
End With
rSource.Copy
rTarget.PasteSpecial xlPasteValues
rSource.Offset(0, 5).Copy
rTarget.Offset(0, 1).PasteSpecial xlPasteValues
rSource.Offset(0, 2).Copy
rTarget.Offset(0, 2).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Re: Code copy data for Report
Posted: Sat Jan 11, 2014 11:19 pm
by snasui

ลองปรับ Code
Set rSource = .Range("a10", .Range("a163").End(xlUp)) เป็น
Set rSource = .Range("a10", .Range("a10").End(xlDown)) ครับ
Re: Code copy data for Report
Posted: Sun Jan 12, 2014 5:08 pm
by yukioh
ขอขอบพระคุณมากครับ