ใส่หัวท้ายกระดาษแล้ว Run Macro แล้วError ครับ
Posted: Sat Dec 17, 2016 10:25 am
สืบเนื่องจากหัวกระทู้นะครับ
ตอนรัน ครั้งแรก โดยจะcopy sheet temp แล้วจึง move เป็น pdf รันผ่านปกติครับ
แต่เนื่องจาก จะแก้แบบ ของ teamp ใหม่โดยเพิ่มหัวท้ายกระดาษเข้ามา ทำให้เกิดerror และ excel ปิดตัวไปครับ
Code ตามนี้เลยครับ
ตอนรัน ครั้งแรก โดยจะcopy sheet temp แล้วจึง move เป็น pdf รันผ่านปกติครับ
แต่เนื่องจาก จะแก้แบบ ของ teamp ใหม่โดยเพิ่มหัวท้ายกระดาษเข้ามา ทำให้เกิดerror และ excel ปิดตัวไปครับ
Code ตามนี้เลยครับ
Code: Select all
Option Explicit
Function FolderExist(Path As String) As Boolean
On Error Resume Next
If Not Dir(Path, vbDirectory) = vbNullString Then
FolderExist = True
End If
On Error GoTo 0
End Function
Sub á¡data()
Dim rall As Range
Dim r As Range
With Sheets("Data")
Set rall = .Range("o2", .Range("o" & .Rows.Count).End(xlUp))
For Each r In rall
Sheets("Temp").Range("b4").Value = r.Value
Sheets("Temp").Copy after:=Sheets("Temp")
ActiveSheet.Name = Range("B4")
ActiveSheet.Move
If FolderExist("D:\Test\" & ActiveSheet.Range("B4").Value & "") Then
ChDir "D:\Test\" & Range("B4").Value & ""
ThisWorkbook.Copy
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"D:\Test\" & ActiveSheet.Range("B4").Value & "\" & Range("B4").Value & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ActiveWorkbook.Close False
Else
On Error Resume Next
MkDir "D:\Test\"
MkDir "D:\Test\" & ActiveSheet.Range("B4").Value & ""
ChDir "D:\Test\" & ActiveSheet.Range("B4").Value & ""
ThisWorkbook.Copy
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"D:\Test\" & ActiveSheet.Range("B4").Value & "\" & Range("B4").Value & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ActiveWorkbook.Close False
End If
Next r
End With
End Sub