หากเราต้องการให้ cell
H8 ของชีท
บันทึกข้อความ ในแต่ละ workbook ตามชื่อไฟล์ ใน
คอลัมน์ A ของชีท
MinGoPP5T1 ไฟล์
ส่งเลขที่นักเรียนเข้าปพ.5.xlsm ที่อยู่ไดร์ฟ
D:\ปพ.5\ปีการศึกษา2564\ประถม\เทอม1-2 รับค่าเลขที่ ที่คัดลอกมาจาก
คอลัมน์ H ชีท
MinGoPP5T1 ของไฟล์
ส่งเลขที่นักเรียนเข้าปพ.5.xlsm เหมือนรูปแบบตามตัวอย่าง เช่น 1, 2, 5, 20, 22, 51 ด้วยการเช็ค Match ชื่อไฟล์ โดยไม่ต้องวางเลขที่กระโดดไปยัง cell อื่น ให้วางได้แค่ cell
H8 ของชีท
บันทึกข้อความ แต่ละ workbook ที่เปิดขึ้นมาเท่านั้น เลขที่สุดท้ายให้ตัดคอมม่าออก เช่น 4, 5, 9, 16
โมดูลที่ใช้คือ Module4
Code: Select all
Sub SendDataNunberMinToPP5pratom_Click()
Dim p, directory As String, fileName As String
Dim sheet As Worksheet, j, i As Integer 'r
Dim tempBook As Workbook, thsBook As Workbook
Dim bookStr As String, rw As Integer
Dim c As Integer
Dim r As Range
Application.ScreenUpdating = False
Set thsBook = ThisWorkbook
p = Range("J1")
' directory = Sheets(p).Range("I2").Value
' fileName = Dir(directory & "*.xl??")
For Each r In thsBook.Sheets(p).Range("I2:I25")
directory = r.Value
fileName = Dir(directory & "*.xl*")
' Set thsBook = ThisWorkbook
j = 7
i = 3
Do While fileName <> ""
Set tempBook = Workbooks.Open(directory & fileName)
bookStr = VBA.Left(tempBook.Name, 8)
'On Error Resume Next
With thsBook.Sheets(p) 'p
rw = Application.Match(bookStr, .Range("A3:A2000"), 0) - 1
c = Application.CountIf(.Range("A3:A2000"), bookStr)
If Err <> 0 Then
MsgBox "File " & tempBook.Name & " not found in Rows 1."
Err = 0
Else
With tempBook.Sheets("บันทึกข้อความ")
.Range("H8:H8").Resize(c, 1).Value = _
thsBook.Worksheets("MinGoPP5T1").Cells(i + rw, j).Resize(c, 1).Value
' ต้องการให้เลขที่เรียงต่อกัน ในเซลเดียวกัน โดยมีคอมม่า กั้นหลังเลขที่ แต่เลขที่สุดท้ายให้ตัดคอมม่าออก
End With
End If
End With
tempBook.Close True
' tempBook.Close False
fileName = Dir()
Loop
' MsgBox ("ส่งเลขที่ เข้าชีทบันทึกข้อความ Directory " & r.Value & " เรียบร้อยแล้วค่ะ")
Next r
Application.ScreenUpdating = True
End Sub
เราต้องปรับโค๊ดอย่างไรครับ เพื่อให้ได้ข้อมูล ตามที่ยกตัวอย่างข้างต้นครับผม
You do not have the required permissions to view the files attached to this post.