1. 程式人生 > >中燃料場報表生成器--入庫報表

中燃料場報表生成器--入庫報表

Option Explicit

Sub CmdGroup2()

 ' 判斷當前資料表是否為進銷存的銷售明細表
    
    If Range("A1") <> "進貨明細表" Then
        MsgBox "當前資料表不是 《進貨明細表》 或者已經被修改,請確認!"
        End '結束程式的執行
    End If
    
    
    ' 新建一個數據表,位於Sheet1後面

    
    If Sheets(Sheets.Count).Name = "料場入庫明細" Then
        MsgBox "料場入庫明細 資料表已經存在,刪除後可重新建立"
        End
    End If

    Sheets.Add After:=Sheets(1)
    ActiveWorkbook.ActiveSheet.Name = "料場入庫明細"
    
    
    '合併後居中單元格
    Range("A1:N1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    Selection.Merge
    
    Range("A1") = "材料入庫明細表"
    
    
    '填寫表頭
    Range("A2") = "序號"
    Range("B2") = "入庫日期"
    Range("C2") = "紙質出庫單編號"
    Range("D2") = "採購網出庫單編號"
    Range("E2") = "物資編碼"
    Range("F2") = "物資名稱"
    Range("G2") = "單位"
    Range("H2") = "入庫數量"
    Range("I2") = "含稅單價"
    Range("J2") = "含稅金額"
    Range("K2") = "其它費用"
    Range("L2") = "供應商"
    Range("M2") = "庫房名稱"
    Range("N2") = "備註"
    
    '設定表頭格式
    Rows("1:1").RowHeight = 22.5
    Range("A1:N1").Font.Size = 18
    Range("A2:N2").Font.Size = 14
    Range("A2:N2").Font.Bold = True
    
    With Range("A2:N2").Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0.499984740745262
        .PatternTintAndShade = 0
    End With
    With Range("A2:N2").Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    
    '根據單元格的內容自動調整單元格大小
    Cells.EntireColumn.AutoFit
    Cells.EntireRow.AutoFit
    
    '檢視銷售明細表一共記錄了多少行
    Dim mItemCount As Integer
    mItemCount = ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
    
    '需要的資料為第13行~mItemCount-1行,複製到對應的表中
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 2), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 2)).Copy (ActiveSheet.Range("C3"))   '單據編號
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 23), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 23)).Copy (ActiveSheet.Range("B3"))   '單據日期
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 6), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 6)).Copy (ActiveSheet.Range("E3"))   '物資編碼
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 4), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 4)).Copy (ActiveSheet.Range("F3"))   '名稱
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 10), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 10)).Copy (ActiveSheet.Range("G3")) '單位
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 12), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 12)).Copy (ActiveSheet.Range("H3")) '數量
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 14), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 14)).Copy (ActiveSheet.Range("I3")) '單價
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 15), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 15)).Copy (ActiveSheet.Range("J3")) '金額
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 20), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 20)).Copy (ActiveSheet.Range("L3"))   '來往單位
    ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(13, 17), ActiveWorkbook.Sheets(1).Cells(mItemCount - 1, 17)).Copy (ActiveSheet.Range("M3"))   '庫房名稱
    
    
    '填寫序號
    Dim i As Integer
    For i = 3 To mItemCount - 11 Step 1
        Cells(i, 1) = i - 2
    Next i
    


End Sub