1. 程式人生 > >中燃料場報表儲存到檔案--出庫報表

中燃料場報表儲存到檔案--出庫報表

 

 

Sub CmdGroup1Save()
'
' 儲存出庫報表到檔案
'


    '判斷當前資料表是否為剛生產的出庫報表

    If Range("A1") <> "材料出庫明細表" Then
        MsgBox "當前資料表不是 《材料出庫明細表》,請確認!"
        End '結束程式的執行
    End If

    
    '在桌面上建立需要儲存檔案的資料夾
    
    Dim mFolderPath As String
        mFolderPath = "C:\Users\Hlj\Desktop\出入庫報表" + Format(Date, "m-d")
        
    If Dir(mFolderPath, vbDirectory) = "" Then
        MkDir mFolderPath
    End If


    '建立需要的報表檔案
    Dim mFilePath As String
        mFilePath = mFolderPath + "\廣宗-出庫" + Format(Date, "m-d") + ".xls"
        
        
    '當前資料夾的名字
    Dim mFileName As String
    mFileName = ActiveWorkbook.Name

    '如果檔案已經存在就刪除已經存在的檔案
    If Dir(mFilePath) <> "" Then
        Kill mFilePath
'        MsgBox "已經刪除存在的檔案"
    End If
    
    Dim mNewBook As Workbook
    Set mNewBook = Workbooks.Add
    With mNewBook
'        .Title = "All Sales"
'        .Subject = "Sales"
        .SaveAs Filename:=mFilePath
    End With
    
    'MsgBox mFileName

    '複製資料並儲存
     Workbooks(mFileName).Sheets("廣宗料場出庫明細").Cells.Copy ActiveWorkbook.Sheets("sheet1").Range("a1")
     ActiveWorkbook.Save
     ActiveWorkbook.Close

    MsgBox mFilePath + " 檔案已經儲存"
End Sub