เรียนอาจารย์ครับ จะเขียน VBA อย่างไร เมื่อ i=3
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
N3 เข้าไปที่ชีท Home ลง Cell
C9
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
O3 เข้าไปที่ชีท Home ลง Cell
C10
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
P3 เข้าไปที่ชีท Home ลง Cell
C11
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
Q3 เข้าไปที่ชีท Home ลง Cell
C12
เสร็จ 1 ไฟล์ที่สร้างเสร็จ ข้อมูลต้องตรงกันในแถวนั้นๆ
พอสร้างไฟล์ต่อไปก็ เพิ่มให้เพิ่มค่า i
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
N4 เข้าไปที่ชีท Home ลง Cell
C9
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
O4 เข้าไปที่ชีท Home ลง Cell
C10
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
P4 เข้าไปที่ชีท Home ลง Cell
C11
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
Q4 เข้าไปที่ชีท Home ลง Cell
C12
เสร็จ 1 ไฟล์ที่สร้างเสร็จ ข้อมูลต้องตรงกันในแถวนั้นๆ
พอสร้างไฟล์ต่อไปก็ เพิ่มให้เพิ่มค่า i
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
N5 เข้าไปที่ชีท Home ลง Cell
C9
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
O5 เข้าไปที่ชีท Home ลง Cell
C10
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
P5 เข้าไปที่ชีท Home ลง Cell
C11
ให้นำข้อมูลเข้าที่ชีท copyrenamefiles Cell
Q5 เข้าไปที่ชีท Home ลง Cell
C12
ทำแบบที่กล่าวมาไปเรื่อย ฯ จนกว่าสร้างไฟล์เสร็จ
(ข้อมูลที่นำไปใส่ที่ชีท Home ต้องเป็นข้อมูลในแถวที่สร้างไฟล์ จากชีท copyrenamefiles (คอลัมภ์ N,O,P,Q))
VAB เป็นดังนี้ครับ Directory คือ D\:ปพ.5\ปีการศึกษา2562\มัธยม\เทอม2\
Code: Select all
Sub CopyDataRenameFiles()
Dim src As String, dst As String, fl As String
Dim rfl As String, rall As Range, r As Range
Dim directory As String, fileName As String, room As String
Dim sheet As Worksheet, j, i As Integer
Dim tempBook As Workbook, thsBook As Workbook
Set thsBook = ThisWorkbook
With ActiveSheet
'Source directory ไดร์ฟปลายทาง
src = .Range("B3").Value
'Destination directory
'dst = Range("D3")
'File name
fl = .Range("B6").Value
'Rename file
' rfl = Range("F3")
Set rall = .Range("d3", .Range("d" & .Rows.Count).End(xlUp))
' On Error Resume Next
Application.ScreenUpdating = False
For Each r In rall
dst = r.Value
rfl = r.Offset(0, 2).Value
FileCopy src & fl, dst & rfl
Set tempBook = Workbooks.Open(fileName:=dst & rfl)
' tempBook.Sheets("นักเรียน").Range("c6:g60").Value = '_
' thsBook.Sheets(r.Offset(0, 4).Value).Range("b3:g57").Value
tempBook.Sheets("นักเรียน").Range("c6:c60").Value = _
thsBook.Sheets(r.Offset(0, 4).Value).Range("b3:b57").Value
tempBook.Sheets("นักเรียน").Range("d6:d60").Value = _
thsBook.Sheets(r.Offset(0, 4).Value).Range("g3:g57").Value
' tempBook.Sheets("นักเรียน").Range("d6:d60").NumberFormat = "0000000000000"
tempBook.Sheets("นักเรียน").Range("e6:e60").Value = _
thsBook.Sheets(r.Offset(0, 4).Value).Range("c3:c57").Value
tempBook.Sheets("นักเรียน").Range("f6:f60").Value = _
thsBook.Sheets(r.Offset(0, 4).Value).Range("d3:d57").Value
tempBook.Sheets("นักเรียน").Range("g6:g60").Value = _
thsBook.Sheets(r.Offset(0, 4).Value).Range("e3:e57").Value
' นำข้อมูลเข้าที่ชีท copyrenamefiles เข้าไปที่ชีท Home ของแต่ละไฟล์ที่สร้างเสร็จ
i = 3
If i = 3 Then
tempBook.Sheets("Home").Range("C9").Value = _
thsBook.Sheets("copyrenamefiles").Range("N" & i).Value
tempBook.Sheets("Home").Range("C10").Value = _
thsBook.Sheets("copyrenamefiles").Range("O" & i).Value
tempBook.Sheets("Home").Range("C11").Value = _
thsBook.Sheets("copyrenamefiles").Range("P" & i).Value
tempBook.Sheets("Home").Range("C12").Value = _
thsBook.Sheets("copyrenamefiles").Range("Q" & i).Value
End If
tempBook.Close True
If Err.Number <> 0 Then
MsgBox "Copy error: " & src & "\" & rfl
End If
Next r
i = i + 1
On Error GoTo 0
End With
Application.ScreenUpdating = True
' MsgBox ("สร้างไฟล์ข้อมูลเข้า Directory เรียบร้อยแล้ว")
End Sub
You do not have the required permissions to view the files attached to this post.