รบกวนผู้รู้ช่วยที่ครับ VBAเซฟ File excel และ สร้าง Folder เป็นชื่อใน คอลั่ม
Posted: Tue Mar 24, 2015 9:51 pm
รบกวนอาจารย์และผู้รู้
หน่อยนะครับ
พอดีผมเพิ่งเริ่มหัดเขียน VBA อยากจะเขียนให้ เซฟ File excel และ สร้าง Folder เป็นชื่อใน คอลั่ม AD1:AD26 อะครับ โดยผลรับที่ได้คือ File excel และ Folder ทั้งหมด 26 ชื่อ ตรงกับในคอลั่ม AD1 : AD26 แต่ผมไม่รู้จะเขียนยังไงอะครับ ไม่ค่อยจะมีความรู้ด้านนี้เลยครับเลย ขอรบกวนผู้รู้ช่วยชี้แนะที่นะครับ
Sub SaveExcel()
Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next ' If directory exist goto next line
strDirname = Range("D2").Value ' New directory name
strFilename = Range("D2").Value 'New file name
strDefpath = "D:\GQI PROJECT\92 KI\Maker report\" 'Default path name
If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub
MkDir strDefpath & strDirname
strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
ActiveWorkbook.SaveAs Filename:=strPathname & ".xlsm", _
FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
MsgBox "Excel Compleated : " & Range("D2").Value
End Sub
พอดีผมเพิ่งเริ่มหัดเขียน VBA อยากจะเขียนให้ เซฟ File excel และ สร้าง Folder เป็นชื่อใน คอลั่ม AD1:AD26 อะครับ โดยผลรับที่ได้คือ File excel และ Folder ทั้งหมด 26 ชื่อ ตรงกับในคอลั่ม AD1 : AD26 แต่ผมไม่รู้จะเขียนยังไงอะครับ ไม่ค่อยจะมีความรู้ด้านนี้เลยครับเลย ขอรบกวนผู้รู้ช่วยชี้แนะที่นะครับ
Sub SaveExcel()
Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next ' If directory exist goto next line
strDirname = Range("D2").Value ' New directory name
strFilename = Range("D2").Value 'New file name
strDefpath = "D:\GQI PROJECT\92 KI\Maker report\" 'Default path name
If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub
MkDir strDefpath & strDirname
strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
ActiveWorkbook.SaveAs Filename:=strPathname & ".xlsm", _
FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
MsgBox "Excel Compleated : " & Range("D2").Value
End Sub