สวัสดีค่ะอาจารย์และทุกๆคน
ดิฉันมีปัญหากับการสร้างไฟล์ใหม่ โดยอยากให้ ไฟล์ที่สร้างใหม่ ไปอยู่ในไดร์ ที่ได้กำหนด path ไป
ปุ่มที่ 1 Click import file
กำหนดให้ผู้ใช้ ระบุที่อยู่ของไฟล์และกดปุ่ม ข้อมูลจะถูก import มาลง sheet2 (ตามรูป)

- 1.png (17.28 KiB) Viewed 309 times
Sheet2 แสดงข้อมูลที่ Import เข้ามา (ตามรูป)

- 2.png (34.14 KiB) Viewed 309 times
ปุ่มที่ 2 ให้ผู้ใช้คลิกเพื่อทำการสร้างไฟล์ใหม่ โดยแยกข้อมูลตาม Column B
เมื่อผู้ใช้คลิก
ปุ่ม (2) Create File
ผลลัพธ์จะได้ดังรูป

- 3.png (34.35 KiB) Viewed 309 times
**** ซึ่งข้อมูลถูกแล้ว แต่อยากให้ไฟล์ที่วาง มาวางไว้ในโฟรเดอร์เดียวกันกับที่ผู้ใช้วางมา คือ C:\Users\PUR_Kannaree\Desktop\test
จะทำต้องแก้ไข Code ตรงส่วนไหนค่ะ
Code: Select all
Sub Button3_Click()
Dim thisWB As String
Dim newWB As String
thisWB = ActiveWorkbook.Name
On Error Resume Next
Sheets("tempsheet").Delete
On Error GoTo 0
Sheets.Add
ActiveSheet.Name = "tempsheet"
Sheets("Sheet2").Select
If ActiveSheet.AutoFilterMode Then
Cells.Select
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
End If
Columns("B:B").Select
Selection.Copy
Sheets("tempsheet").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
If (Cells(1, 1) = "") Then
lastrow = Cells(1, 1).End(xlDown).Row
If lastrow <> Rows.count Then
Range("A1:A" & lastrow - 1).Select
Selection.Delete Shift:=xlUp
End If
End If
Columns("A:A").Select
Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("B1"), Unique:=True
Columns("A:A").Delete
Cells.Select
Selection.Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lMaxSupp = Cells(Rows.count, 1).End(xlUp).Row
For suppno = 2 To lMaxSupp
Windows(thisWB).Activate
supName = Sheets("tempsheet").Range("A" & suppno)
If supName <> "" Then
Workbooks.Add
ActiveWorkbook.SaveAs supName
newWB = ActiveWorkbook.Name
Windows(thisWB).Activate
Sheets("Sheet2").Select
Cells.Select
If ActiveSheet.AutoFilterMode = False Then
Selection.AutoFilter
End If
Selection.AutoFilter Field:=2, Criteria1:="=" & supName, _
Operator:=xlAnd, Criteria2:="<>"
lastrow = Cells(Rows.count, 2).End(xlUp).Row
Rows("1:" & lastrow).Copy
Windows(newWB).Activate
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
End If
Next
Sheets("tempsheet").Delete
Sheets("Sheet2").Select
If ActiveSheet.AutoFilterMode Then
Cells.Select
ActiveSheet.ShowAllData
End If
End Sub
ขอบคุณค่ะ