Page 1 of 1

การปิดไฟล์ ด้วย VBA

Posted: Thu Jun 08, 2017 1:58 pm
by pondslam
ผมต้องการที่จะปิดไฟล์ที่ขึ้นต้นด้วย "DEAC" ตัวอย่างชื่อไฟล์ DEAC_xx_xx_170501
แต่ไฟล์นี้ทั้งหมดเป็นไฟล์ .CSV ครับ ไม่สามารถผมจะต้องพิมพ์ Code ยังไงครับ
อนึ่ง Code VBA ข้างล่างนี้ประยุกต์มาจากอันเก่าครับ ไม่ได้สร้างเอง

ขอบคุณครับ

Sub Mainsub2()
Application.ScreenUpdating = False
If MsgBox("Close All Files, Right?", vbYesNo) = vbYes Then
Dim cur As Integer
Dim actWBName As String
Dim iNo As Double

cur = Workbooks.Count
'For iNo = 1 To cur
iNo = 1
Do Until iNo > cur
'Workbooks(iNo).Activate
actWBName = Workbooks(iNo).Name
If Left(actWBName, 23) = "DEAC" Then
Workbooks(iNo).Close
cur = cur - 1
iNo = iNo - 1
End If
'Next iNo
iNo = iNo + 1
Loop
MsgBox ("Completed.")
End If
Application.ScreenUpdating = True
End Sub

Re: การปิดไฟล์ ด้วย VBA

Posted: Thu Jun 08, 2017 2:04 pm
by pondslam
แก้ไขได้แล้วครับ ผมแก้ตรงนี้ครับ จาก 23 เปลี่ยนเป็น 4
If Left(actWBName, 23) = "DEAC" Then

สามารถเอาไปประยุกต์ได้ครับ