Page 1 of 1

สอบถาม CodeVB Save As เป็นไฟล์ .xls

Posted: Sun Mar 17, 2024 9:43 pm
by tigerwit
จากไฟล์ที่แนบมา
ต้องการ save ข้อมูลจากชีท Grade ไปสร้างไฟล์ใหม่ .xls โดยไปเก็บไว้ในชื่อและ โฟลเดอร์ที่กำหนดไว้
ปัญหาคือ เมื่อส่งออกไฟล์ไปแล้ว จะได้ไฟล์ที่มีไอคอน ที่ไม่ใช่ .xls
จะต้องปรับโค๊ดอย่างไรครับ จึงจะได้ไฟล์ใหม่เป็น .xls

Code: Select all

Sub ExpGPA()
    Dim sFolderPath As String
    Dim wb1 As Workbook
    Dim ws1 As Worksheet
    Dim wb2 As Workbook
    Dim Path As String
    Dim FName As String
'    Dim FileSaveName As Variant
'    FileSaveName = Application.GetSaveAsFilename(InitialFileName:=ActiveWorkbook.Name, FileFilter:="Excel 2003 (*.xls), *.xls")
    On Error Resume Next
    Application.ScreenUpdating = False
    sFolderPath = "C:\" & Range("J1").Value
        If Dir(sFolderPath, vbDirectory) = "" Then
            MkDir sFolderPath
        End If
    sFolderPath = "C:\" & Range("J1").Value & "\" & "LocalSchool"
        If Dir(sFolderPath, vbDirectory) = "" Then
            MkDir sFolderPath
        End If
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Sheets("Grade")
    
    FName = ws1.Range("K1")
    ws1.Range("A:G").Copy
    Set wb2 = Workbooks.Add
    With wb2.ActiveSheet.Range("A:G")
        .PasteSpecial (xlValues)
        .PasteSpecial (xlFormats)
    Range("A2").Select
    Application.DisplayAlerts = True
    End With
    Application.DisplayAlerts = False
    wb2.SaveAs Filename:=sFolderPath & "\" & FName, FileFormat:=56
    Application.CutCopyMode = False
    wb2.Close
    Application.ScreenUpdating = True
            If MsgBox("ส่งออกไฟล์ชื่อ " & FName & vbCrLf & "ไปไว้ที " & "C:\" & Range("J1").Value & "\" & "LocalSchool" _
            & " เรียบร้อยแล้ว" & vbCrLf & "ต้องการเปิด Folder กด Yes ไม่ต้องการ กด No ", 36, "Open Folder") = 6 Then
            ActiveWorkbook.FollowHyperlink Address:=sFolderPath, NewWindow:=True
        End If
End Sub


Re: สอบถาม CodeVB Save As เป็นไฟล์ .xls

Posted: Mon Mar 18, 2024 10:22 am
by logic
ลองเพิ่มนามสกุลไฟล์เข้าไปช่วยดูครับ

wb2.SaveAs Filename:=sFolderPath & "\" & FName & ".xls", FileFormat:=56