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

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

Option Explicit


Sub CmdGroup3Save()

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

    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 + "\" + ThisWorkbook.Sheets("配置").Range("A1").Value + "-庫存" + Format(Date, "m-d") + ".xlsx"
   
    '當前資料夾的名字
    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