Excel VBA @竣工文件

範例影片:

前言:

工程報竣工後會有一堆文件需要輸出給上級機關簽核,尤其是當下載下來後都是分散的話,每個輸出都要點擊一次列印、調整列印份數,可是會相當麻煩,因此寫了一個VBA來簡化這些問題。
圖一、上級機關需要的文件的一部分。
 使用方法:

  1. 必須先將所有要輸出的文件與"竣工派驗VBA.xlsm"放在同一個資料夾
  2. 檢查該文件的工作表名稱及相對應的文件編號(需要在程式碼  Arrange 中更改)。
  3. 若有資料先清除資料。
  4. 詳細檢查各個文件的內容正確性。
  5. 列印。
圖二、Arrange中的一部分程式碼(arr與arr2數量需要相同,否則會出錯)
結論:

因應每家機關需要的文件不同,需要透過更改程式碼量身打造需要的文件名稱與編號,故提供的程式碼僅供參考使用。

程式碼:
Sub main()

Call GetFile

Call MoveData

Call Arrange

Call sortvba

Sheets(1).Select

End Sub

Sub GetFile()

Path = ThisWorkbook.Path '相對路徑

ChDir Path

f = Dir("")

Do Until f = ""

    If Not f Like "竣工派驗VBA*" Then
    
        Workbooks.Open f, False
    
    End If

    f = Dir()

Loop

End Sub

Sub MoveData()

For Each Workbook In Workbooks

    wbn = Workbook.Name
    
    Workbooks(wbn).Activate
    
    If Not wbn Like "竣工派驗VBA*" Then
    
        i = 0
    
        For Each Sheet In Sheets
        
            wpt = InStr(1, wbn, ".")
        
            If wpt <> 0 Then shn = Mid(wbn, 1, wpt - 1)
        
            pt = InStr(1, wbn, "_")
            
            If pt <> 0 Then shn = Mid(wbn, 1, pt - 1)
        
            If Sheets.Count = 1 And i = 0 Then
            
                Sheets(1).Name = shn
            
            Else
                
                i = i + 1
                Sheet.Name = shn & "-" & i
            
            End If
        
            Sheet.Move after:=Workbooks(1).Sheets(Sheets.Count)
        
        Next

    End If

Next

End Sub

Sub DeleteData()

For Each Sheet In Sheets

If Sheet.Index <> 1 Then

Application.DisplayAlerts = False

Sheet.Delete

Application.DisplayAlerts = True

End If

Next

End Sub

Sub Arrange()

Dim OrderArr(0 To 15, 1 To 2) As Variant

arr = Array("施工過程異動紀錄統計表", "施工品質評量表", "工程驗收單", "工程請款黏貼紙" _
            , "工程興辦成本明細表", "工程結算驗收證明書", "工程移管清冊-2", "工程移管清冊-1" _
            , "工程報廢單", "工程初驗、驗收、再驗紀錄表", "工程保固切結書", "竣工日期書面通知單")

arr2 = Array(3, 14, 8, 5, 13, 9, "16-2", "16-1", 12, 7, 15, 1)

For i = 0 To UBound(arr)

    OrderArr(i, 1) = arr(i)
    OrderArr(i, 2) = arr2(i)
    
Next

For Each Sheet In Sheets

    If Sheet.Index > 1 Then
    
        For i = 0 To UBound(arr)
        
            If Sheet.Name = OrderArr(i, 1) Then
            
                Sheet.Name = OrderArr(i, 2)
        
                Exit For
                
            End If
        Next
    
    End If

Next

End Sub

Sub printvba()

Dim FileName As String
Dim FIleCopies As Integer
Dim num As Integer

FinalType = InputBox("請問你的工程經費來源是?" & vbCrLf & "1.農委會補助" & vbCrLf & "2.自籌款", "竣工派驗VBA")

msg = MsgBox("請問要預覽列印後再印嗎?(Y/N)", vbYesNo)

If FinalType = 1 Then

    PrintCol = 9

ElseIf FinalType = 2 Then

    PrintCol = 12

End If

For Each Sheet In Sheets
    
    IsPrint = False

    dashLocation = InStr(1, Sheet.Name, "-")
        
    If dashLocation <> 0 Then
        
        num = Mid(Sheet.Name, 1, dashLocation - 1)

        IsPrint = True
        
    ElseIf IsNumeric(Sheet.Name) Then
    
        num = Val(Sheet.Name)
    
        IsPrint = True
    
    End If
    
    If IsPrint = True Then
    
        With Sheets(1)
            
            r = 5
            
            If .Range("W" & r + num) = "#" Then
                
                FIleCopies = .Cells(r + num, PrintCol)
            
            Else
        
                FIleCopies = .Cells(r + num, PrintCol) + 2
        
            End If
            
            FileName = .Cells(r + num, 2)
            
        End With
            
        Debug.Print FileName & ":" & FIleCopies

        If FIleCopies <> 0 Then
        
            Sheet.Activate
        
            If msg = vbYes Then
        
                Sheet.PrintOut copies:=FIleCopies, preview:=True
        
            Else
            
                Sheet.PrintOut copies:=FIleCopies, preview:=False
            
            End If
        
        End If

    End If

Next

End Sub

Sub sortvba()

For i = 1 To 16

    For Each Sheet In Sheets
    
        shn = Sheet.Name
        
        If shn Like "*-*" Then
        
            pt = InStr(1, shn, "-")
            
            shn = Mid(shn, 1, pt - 1)
        
            If Val(shn) = i Then Sheet.Move after:=Sheets(Sheets.Count)
        
        End If
        
        If IsNumeric(shn) Then
        
            If Val(shn) = i Then Sheet.Move after:=Sheets(Sheets.Count)
        
        End If
        
    Next

Next

End Sub



留言

Popular Posts

Excel VBA @ 監造日報表、查驗表 -2

ExcelVBA@施工照片整理的應用範例

Excel VBA@ 監造日報表、查驗表