การนำข้อมูล Excel หลาย ๆ ไฟล์มาต่อกัน

ในที่นี่จะใช้ VBA ในการช่วยทำ ซึ่งจะง่ายมาก ๆ สำหรับบทความนี้ใช้ Excel 2016 ในการทำ ผมมีไฟล์ทั้งหมด 22 ไฟล์ ต้องการ copy เข้าไปอยู่ใน Sheet ...

ในที่นี่จะใช้ VBA ในการช่วยทำ ซึ่งจะง่ายมาก ๆ สำหรับบทความนี้ใช้ Excel 2016 ในการทำ ผมมีไฟล์ทั้งหมด 22 ไฟล์ ต้องการ copy เข้าไปอยู่ใน Sheet เดียวกัน (ทุกไฟล์มีจำนวนคอลัมภ์เหมือนกัน) ถ้าจะ Copy เองนี่คงต้องขยันกันหน่อย

ลองนึกดูว่าถ้ามีไฟล์มากกว่านี้จะเป็นอย่างไร...
เริ่มกันเลยดีกว่าครับ

1. เปิด Excel ขึ้นมาก่อนเลยครับ
2. ไปที่เมนู Developer คลิกเลือก Visual Basic จะได้หน้าต่างของ Visual Basic ขึ้นมา
ถ้า Excel ของท่านไม่มีเมนู Developer ก็ให้ไปที่ File >> Option เลือก Costomize Ribbon ที่ Main Tabs ให้ติ๊กถูกหัวข้อ Developer

3.  คลิกไอคอนตามภาพ เลือก Module

จะได้หน้าต่างสำหรับพิมพ์โค๊ด VB


Sub GetSheets()
Path = "C:\cwt33\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub 
พิมพ์ code เสร็จแล้ว กด Run Code โปรแกรมก็จะนำทุก Sheets ที่มีอยู่ในแต่ละไฟล์มารวมกันไว้ที่ไฟล์เดียว
 ในที่นี้ไฟล์ผมเก็บไว้ที่ไดร์ c:\cwt33  และทุกไฟล์ นามสกุล .xlsx
4. ทีนี้เราก็จะรวมทุก Sheets ให้เป็น Sheet เดียวโดยเปลี่ยน VBA Code เป็น


Sub CollectData()
Dim ws As Worksheet
Dim r As Range
Dim rTarget As Range
Dim DataAll As String
DataAll = "Sheet1"
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name <> DataAll Then
With Sheets(DataAll)
Set rTarget = .Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End With
Set r = ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp))
r.SpecialCells(xlCellTypeConstants).EntireRow.Copy
rTarget.PasteSpecial xlPasteValues
End If
Next ws
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub 
เสร็จแล้วกด Run เหมือนเดิม Code ก็จะเอาข้อมูลทุก Sheets มารวมกันไว้ที่ Sheet เดียวให้โดยอัตโนมัติ

COMMENTS

BLOGGER: 1
  1. สวัสดีค่ะ รบกวนสอบถามเพิ่มเติม ไม่ทราบว่าตรงช่วงของ code r.SpecialCells(xlCellTypeConstants).EntireRow.Copy
    rTarget.PasteSpecial xlPasteValues
    หมายถึงอะไรหรอคะ

    ตอบลบ

{getFeatured} $label={recent}
ชื่อ

ข่าวไอที,18,Android,34,Blogger,10,Books,1,Gadgets,1,Health,2,Huawei,9,I9100T,1,Joomla,2,Motorcycle,1,Motoring,11,Other,10,P8,9,Pretty,2,S2,1,Software,11,Statistics,3,Tip,20,Travel,1,Tutorial,6,Webmaster,2,
ltr
item
MzA Blog: การนำข้อมูล Excel หลาย ๆ ไฟล์มาต่อกัน
การนำข้อมูล Excel หลาย ๆ ไฟล์มาต่อกัน
https://4.bp.blogspot.com/-fMlj1Gl3E_o/W8B1uoVVoBI/AAAAAAAALVY/aNJIWuQE7ksc5ag2yEZXg2WPbQ2U7SONQCLcBGAs/s640/excel-001.JPG
https://4.bp.blogspot.com/-fMlj1Gl3E_o/W8B1uoVVoBI/AAAAAAAALVY/aNJIWuQE7ksc5ag2yEZXg2WPbQ2U7SONQCLcBGAs/s72-c/excel-001.JPG
MzA Blog
http://msawisit.blogspot.com/2018/11/excel.html
http://msawisit.blogspot.com/
http://msawisit.blogspot.com/
http://msawisit.blogspot.com/2018/11/excel.html
true
3606440550594326737
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content