snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ต้องการ Save as เป็นชื่อไฟล์วันเวลา และ close ไฟล์ Excel หลายๆไฟล์ แต่ปัญหาที่เจอคือสามารถปิดได้เพียงทีละ 1 ไฟล์เท่านั้น
Sub Save_and_close_all()
Dim wb As Workbook
Dim sPath As String
'Edit path Here
'=========================================
sPath = "C:\testsave\"
'=========================================
For Each wb In Application.Workbooks
If wb.Name <> ThisWorkbook.Name Then
If InStr(Right(wb.Name, 5), ".xls") > 0 Then
wb.Close SaveChanges:=True
Else
wb.Close SaveChanges:=True, _
Filename:=sPath & Format(Now, " yyyy-mm-dd-hhmm")
End If
End If
Next wb
End Sub
You do not have the required permissions to view the files attached to this post.
For Each wb In Application.Workbooks
If wb.Name <> ThisWorkbook.Name Then
If InStr(Right(wb.Name, 5), ".xls") > 0 Then
wb.SaveAs Filename:=sPath & Format(Now, " yyyy-mm-dd-hhmm"), FileFormat:=xlWorkbookDefault
wb.Close
End If
End If
Next wb