Option Strict Off
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Public Class 訂單價格利潤明細表
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet
    Dim dr As SqlDataReader
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    ReadOnly BB(3) As Boolean : Dim WW As String : Public 平均數 As Integer : Public 最終數 As Integer = 0 : Public 資料數 As Integer = 0 : Dim NUM2 As Integer : Dim N1 As Integer
    ReadOnly MYSQL1 As String = "SELECT 訂單控制表.BUYER, 訂單控制表.年份季度, 訂單控制表.訂單流水號, 訂單控制表.客戶訂單號, 訂單控制表.型體號碼, 訂單控制表.GOODBOX AS 型體名稱, 訂單控制表.APMA確認 AS 顏色, 
                                   訂單控制表.TVW AS 楦型, 訂單控制表.訂單數量, 訂單控制表.客戶交期 AS 交期開始, 訂單控制表.客戶交期1 AS 交期結束, 訂單控制表.單價, 訂單控制表.總金額 AS 銷售金額, 
                                   訂單控制表.備註, 訂單控制表.生產工廠, 訂單控制表.資料流水號, 訂單價格利潤明細表.Quantity, 訂單價格利潤明細表.Currency, 訂單價格利潤明細表.Exchange, 
                                   訂單價格利潤明細表.Price, 訂單價格利潤明細表.Discount_price AS [Discount price], 訂單價格利潤明細表.Commercial_invoice AS [Commercial invoice], 
                                   訂單價格利潤明細表.Overage_invoice AS [Overage invoice], 訂單價格利潤明細表.Selling_price AS [Selling price], 訂單價格利潤明細表.HTS_code AS [HTS code], 
                                   訂單價格利潤明細表.Remark1, 訂單價格利潤明細表.Fty_price AS [Fty price], 訂單價格利潤明細表.Mold_cost AS [Mold cost], 
                                   訂單價格利潤明細表.Collected_mold_cost AS [Collected mold cost], 訂單價格利潤明細表.Amortization_qty AS [Amortization qty], 訂單價格利潤明細表.Profit, 
                                   訂單價格利潤明細表.Commission, 訂單價格利潤明細表.Remark2, 訂單價格利潤明細表.Price AS 客戶付款, 訂單價格利潤明細表.Price AS 未收款項, 
                                   訂單價格利潤明細表.Price AS 預收款項, 訂單價格利潤明細表.Price AS 成本金額, 訂單價格利潤明細表.Price AS 利潤, 訂單價格利潤明細表.Price AS 實際收款, 
                                   訂單價格利潤明細表.年份, 訂單價格利潤明細表.利潤表流水號, 訂單價格利潤明細表.審核
                             FROM  訂單控制表 LEFT OUTER JOIN 訂單價格利潤明細表 ON 訂單控制表.資料流水號 = 訂單價格利潤明細表.資料流水號 "
    ReadOnly MYSQL2 As String = "SELECT     訂單控制表.BUYER, 訂單控制表.年份季度, 訂單控制表.訂單流水號, 訂單控制表.客戶訂單號, 訂單控制表.型體號碼, 訂單控制表.GOODBOX AS 型體名稱, 
                                       訂單控制表.APMA確認 AS 顏色, 訂單控制表.TVW AS 楦型, 訂單控制表.訂單數量, 訂單控制表.客戶交期 AS 交期開始, 訂單控制表.客戶交期1 AS 交期結束, 訂單控制表.單價, 
                                       訂單控制表.總金額 AS 銷售金額, 訂單控制表.備註, 訂單控制表.生產工廠, 訂單控制表.資料流水號, 訂單價格利潤明細表.Quantity, 訂單價格利潤明細表.Currency, 
                                       訂單價格利潤明細表.Exchange, 訂單價格利潤明細表.Price, 訂單價格利潤明細表.Discount_price AS [Discount price], 
                                       訂單價格利潤明細表.Commercial_invoice AS [Commercial invoice], 訂單價格利潤明細表.Overage_invoice AS [Overage invoice], 
                                       訂單價格利潤明細表.Selling_price AS [Selling price], 訂單價格利潤明細表.HTS_code AS [HTS code], 訂單價格利潤明細表.Remark1, 
                                       訂單價格利潤明細表.Fty_price AS [Fty price], 訂單價格利潤明細表.Mold_cost AS [Mold cost], 訂單價格利潤明細表.Collected_mold_cost AS [Collected mold cost], 
                                       訂單價格利潤明細表.Amortization_qty AS [Amortization qty], 訂單價格利潤明細表.Profit, 訂單價格利潤明細表.Commission, 訂單價格利潤明細表.Remark2, 
                                       訂單價格利潤明細表.Price AS 客戶付款, 訂單價格利潤明細表.Price AS 未收款項, 訂單價格利潤明細表.Price AS 預收款項, 訂單價格利潤明細表.Price AS 成本金額, 
                                       訂單價格利潤明細表.Price AS 利潤, 訂單價格利潤明細表.Price AS 實際收款, 訂單價格利潤明細表.年份, 訂單價格利潤表暫存檔.利潤表流水號, 訂單價格利潤明細表.審核
                            FROM       訂單控制表 INNER JOIN 訂單價格利潤表暫存檔 ON 訂單控制表.資料流水號 = 訂單價格利潤表暫存檔.資料流水號 LEFT OUTER JOIN
                                       訂單價格利潤明細表 ON 訂單控制表.資料流水號 = 訂單價格利潤明細表.資料流水號
                            ORDER BY   訂單控制表.訂單流水號 DESC"
    Private Sub Set_DGV1載入前設定()
        清單1_dgv.DataSource = Nothing : ds.Clear()
        清單1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單1_dgv.ColumnHeadersHeight = 50
        清單1_dgv.AllowUserToAddRows = False
        清單1_dgv.RowTemplate.Height = 35
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds) : 清單1_dgv.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_訂單清單1()
        Set_DGV1載入前設定()
        If CheckBox1.Checked = True Then
            Timer1.Enabled = True : Button6.Enabled = True : Button5.Enabled = False : Button9.Enabled = False
            Button11.Enabled = True : Button12.Enabled = True : Button13.Enabled = True
            If TextBox1.Text = "" Then
                SQL1 = MYSQL1 & "WHERE    ((訂單控制表.訂單流水號 Like '%" & PA & "%') OR (訂單控制表.年份季度 LIKE '%" & PA & "%') OR (訂單控制表.客戶訂單號 LIKE '%" & PA & "%') OR 
                                           (訂單控制表.型體號碼 Like '%" & PA & "%') OR (訂單控制表.APMA確認 LIKE '%" & PA & "%') OR (訂單控制表.GOODBOX LIKE '%" & PA & "%') OR 
                                           (訂單控制表.TVW Like '%" & PA & "%') OR (訂單控制表.備註 LIKE '%" & PA & "%') OR (訂單控制表.生產工廠 LIKE '%" & PA & "%')) AND 
                                           (訂單價格利潤明細表.訂單流水號 Is Not NULL) AND (訂單價格利潤明細表.CFMSPEC LIKE 'LANDED BRANDED')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            Else
                SQL1 = MYSQL1 & "WHERE     (訂單控制表.訂單流水號 LIKE '" & TextBox1.Text & "') AND (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.CFMSPEC LIKE 'LANDED BRANDED')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            End If
            Set_DGV1載入後設定() : Set_grid() : Set_自動彙總1()
            Set_暫存資料設定() : Set_編輯清單1() : Set_grid() : Set_grid3() : Set_自動彙總1()
        ElseIf CheckBox1.Checked = False Then
            Timer1.Enabled = False : Label47.Visible = False : Button6.Enabled = False : Button5.Enabled = True : Button9.Enabled = True
            Button11.Enabled = False : Button12.Enabled = False : Button13.Enabled = False
            If PA = "" Then
                SQL1 = MYSQL1 & "WHERE     (訂單控制表.CXL IS NULL) OR (訂單控制表.CXL = 0) AND (訂單價格利潤明細表.年份 LIKE '" & ComboBox1.Text & "') AND (訂單控制表.CFMSPEC LIKE 'LANDED BRANDED')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            Else
                SQL1 = MYSQL1 & "WHERE    ((訂單控制表.訂單流水號 Like '%" & PA & "%') OR (訂單控制表.年份季度 LIKE '%" & PA & "%') OR (訂單控制表.客戶訂單號 LIKE '%" & PA & "%') OR 
                                           (訂單控制表.型體號碼 LIKE '%" & PA & "%') OR (訂單控制表.APMA確認 LIKE '%" & PA & "%') OR (訂單控制表.GOODBOX LIKE '%" & PA & "%') OR 
                                           (訂單控制表.TVW LIKE '%" & PA & "%') OR (訂單控制表.備註 LIKE '%" & PA & "%') OR (訂單控制表.生產工廠 LIKE '%" & PA & "%')) AND 
                                           (訂單價格利潤明細表.訂單流水號 IS NOT NULL) AND (訂單價格利潤明細表.年份 LIKE '" & ComboBox1.Text & "') AND 
                                           (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.CFMSPEC LIKE 'LANDED BRANDED')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            End If
            列印用SQL = SQL1
            Set_DGV1載入後設定()
        End If
    End Sub
    Private Sub Set_編輯清單1()
        Set_DGV1載入前設定() : SQL1 = MYSQL2 : Set_DGV1載入後設定() : Me.Button13.PerformClick() : Me.Button11.PerformClick()
        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("Currency").Value = "USD" : 清單1_dgv.Rows(i).Cells("年份").Value = ComboBox1.Text : Next i
    End Sub
    Private Sub Set_DGV3載入前設定()
        清單2_dgv.DataSource = Nothing : ds2.Clear()
        清單2_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單2_dgv.ColumnHeadersHeight = 50
        清單2_dgv.AllowUserToAddRows = False
        清單2_dgv.RowTemplate.Height = 35
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV3載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds2) : 清單2_dgv.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_訂單清單2()
        Set_DGV3載入前設定()
        If CheckBox1.Checked = True Then
            Timer1.Enabled = True : Button6.Enabled = True : Button5.Enabled = False : Button9.Enabled = False
            Button11.Enabled = True : Button12.Enabled = True : Button13.Enabled = True
            If TextBox1.Text = "" Then
                SQL1 = MYSQL1 & "WHERE    ((訂單控制表.訂單流水號 Like '%" & PA & "%') OR (訂單控制表.年份季度 LIKE '%" & PA & "%') OR (訂單控制表.客戶訂單號 LIKE '%" & PA & "%') OR 
                                           (訂單控制表.型體號碼 Like '%" & PA & "%') OR (訂單控制表.APMA確認 LIKE '%" & PA & "%') OR (訂單控制表.GOODBOX LIKE '%" & PA & "%') OR 
                                           (訂單控制表.TVW Like '%" & PA & "%') OR (訂單控制表.備註 LIKE '%" & PA & "%') OR (訂單控制表.生產工廠 LIKE '%" & PA & "%')) AND 
                                           (訂單價格利潤明細表.訂單流水號 Is Not NULL) AND (訂單控制表.CFMSPEC LIKE 'FIRST COST')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            Else
                SQL1 = MYSQL1 & "WHERE     (訂單控制表.訂單流水號 LIKE '" & TextBox1.Text & "') AND (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.CFMSPEC LIKE 'FIRST COST')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            End If
            Set_DGV3載入後設定() : Set_grid3() : Set_自動彙總1()
            Set_暫存資料設定() : Set_編輯清單2() : Set_grid3() : Set_自動彙總1()
        ElseIf CheckBox1.Checked = False Then
            Timer1.Enabled = False : Label47.Visible = False : Button6.Enabled = False : Button5.Enabled = True : Button9.Enabled = True
            Button11.Enabled = False : Button12.Enabled = False : Button13.Enabled = False
            If PA = "" Then
                SQL1 = MYSQL1 & "WHERE     (訂單控制表.CXL IS NULL) OR (訂單控制表.CXL = 0) AND (訂單價格利潤明細表.年份 LIKE '" & ComboBox1.Text & "') AND (訂單控制表.CFMSPEC LIKE 'FIRST COST')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            Else
                SQL1 = MYSQL1 & "WHERE    ((訂單控制表.訂單流水號 Like '%" & PA & "%') OR (訂單控制表.年份季度 LIKE '%" & PA & "%') OR (訂單控制表.客戶訂單號 LIKE '%" & PA & "%') OR 
                                           (訂單控制表.型體號碼 LIKE '%" & PA & "%') OR (訂單控制表.APMA確認 LIKE '%" & PA & "%') OR (訂單控制表.GOODBOX LIKE '%" & PA & "%') OR 
                                           (訂單控制表.TVW LIKE '%" & PA & "%') OR (訂單控制表.備註 LIKE '%" & PA & "%') OR (訂單控制表.生產工廠 LIKE '%" & PA & "%')) AND 
                                           (訂單價格利潤明細表.訂單流水號 IS NOT NULL) AND (訂單價格利潤明細表.年份 LIKE '" & ComboBox1.Text & "') AND 
                                           (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.CFMSPEC LIKE 'FIRST COST')
                                 ORDER BY   訂單控制表.訂單流水號 DESC"
            End If
            列印用SQL = SQL1
            Set_DGV3載入後設定()
        End If
    End Sub
    Private Sub Set_編輯清單2()
        Set_DGV3載入前設定() : SQL1 = MYSQL2 : Set_DGV3載入後設定() : Me.Button13.PerformClick() : Me.Button11.PerformClick()
        For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("Currency").Value = "USD" : 清單2_dgv.Rows(i).Cells("年份").Value = ComboBox1.Text : Next i
    End Sub
    Private Sub Set_暫存資料設定()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "DELETE FROM 訂單價格利潤表暫存檔"
        cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : conn.Close()

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP (1) 利潤表流水號 FROM 訂單價格利潤明細表 ORDER BY 利潤表流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : NUM2 = Strings.Right(dr("利潤表流水號").ToString, 8) : End If
        conn.Close()
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
                Set_暫存資料設定1()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 訂單價格利潤表暫存檔 (資料流水號, 利潤表流水號, 訂單流水號) " &
                   "VALUES ('" & 清單1_dgv.Rows(i).Cells("資料流水號").Value & "','" & WW & "', '" & 清單1_dgv.Rows(i).Cells("訂單流水號").Value & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
                Set_暫存資料設定1()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 訂單價格利潤表暫存檔 (資料流水號, 利潤表流水號, 訂單流水號) " &
                   "VALUES ('" & 清單2_dgv.Rows(i).Cells("資料流水號").Value & "','" & WW & "', '" & 清單2_dgv.Rows(i).Cells("訂單流水號").Value & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Next i
        End If
    End Sub
    Private Sub Set_暫存資料設定1()
        NUM2 += 1
        If NUM2 < 10 Then : WW = "PR" & "0000000" & NUM2
        ElseIf NUM2 > 9 And NUM2 < 100 Then : WW = "PR" & "000000" & NUM2
        ElseIf NUM2 > 99 And NUM2 < 1000 Then : WW = "PR" & "00000" & NUM2
        ElseIf NUM2 > 999 And NUM2 < 10000 Then : WW = "PR" & "0000" & NUM2
        ElseIf NUM2 > 9999 And NUM2 < 100000 Then : WW = "PR" & "000" & NUM2
        ElseIf NUM2 > 99999 And NUM2 < 100000 Then : WW = "PR" & "00" & NUM2
        ElseIf NUM2 > 999999 And NUM2 < 1000000 Then : WW = "PR" & "0" & NUM2
        ElseIf NUM2 > 9999999 Then : WW = "PR" & NUM2
        End If
    End Sub
    Private Sub Set_未建檔清單()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT          訂單控制表.訂單流水號
                FROM            訂單控制表 FULL OUTER JOIN 訂單價格利潤明細表 ON 訂單控制表.資料流水號 = 訂單價格利潤明細表.資料流水號
                WHERE          (訂單控制表.CXL IS NULL OR 訂單控制表.CXL = 0) AND (訂單控制表.CFMSPEC LIKE '" & ComboBox10.Text & "') AND 
                               (訂單價格利潤明細表.資料流水號 IS NULL) AND (訂單控制表.CFMSPEC LIKE '" & ComboBox10.Text & "')
                GROUP BY        訂單控制表.訂單流水號
                ORDER BY        訂單控制表.訂單流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.SelectCommand.CommandTimeout = 900 : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_grid()
        清單1_dgv.Columns(0).ReadOnly = True : 清單1_dgv.Columns(1).ReadOnly = True : 清單1_dgv.Columns(2).ReadOnly = True : 清單1_dgv.Columns(3).ReadOnly = True
        清單1_dgv.Columns(4).ReadOnly = True : 清單1_dgv.Columns(5).ReadOnly = True : 清單1_dgv.Columns(6).ReadOnly = True : 清單1_dgv.Columns(7).ReadOnly = True
        清單1_dgv.Columns(8).ReadOnly = True : 清單1_dgv.Columns(9).ReadOnly = True : 清單1_dgv.Columns(10).ReadOnly = True : 清單1_dgv.Columns(12).ReadOnly = True
        清單1_dgv.Columns(13).ReadOnly = True : 清單1_dgv.Columns(14).ReadOnly = True : 清單1_dgv.Columns(20).ReadOnly = True : 清單1_dgv.Columns(22).ReadOnly = True
        清單1_dgv.Columns(28).ReadOnly = True : 清單1_dgv.Columns(30).ReadOnly = True : 清單1_dgv.Columns(31).ReadOnly = True : 清單1_dgv.Columns(33).ReadOnly = True
        清單1_dgv.Columns(34).ReadOnly = True : 清單1_dgv.Columns(35).ReadOnly = True : 清單1_dgv.Columns(36).ReadOnly = True : 清單1_dgv.Columns(37).ReadOnly = True
        清單1_dgv.Columns(38).ReadOnly = True : 清單1_dgv.Columns(39).ReadOnly = True : 清單1_dgv.Columns(40).ReadOnly = True
        '764
        清單1_dgv.Columns(0).Width = 90 : 清單1_dgv.Columns(1).Width = 100 : 清單1_dgv.Columns(2).Width = 115 : 清單1_dgv.Columns(3).Width = 80
        清單1_dgv.Columns(4).Width = 90 : 清單1_dgv.Columns(5).Width = 90 : 清單1_dgv.Columns(6).Width = 100 : 清單1_dgv.Columns(7).Width = 50
        清單1_dgv.Columns(8).Width = 60 : 清單1_dgv.Columns(9).Width = 70 : 清單1_dgv.Columns(10).Width = 70 : 清單1_dgv.Columns(11).Width = 60
        清單1_dgv.Columns(12).Width = 80 : 清單1_dgv.Columns(13).Width = 100 : 清單1_dgv.Columns(14).Width = 80 : 清單1_dgv.Columns(15).Visible = False
        清單1_dgv.Columns(16).Width = 60 : 清單1_dgv.Columns(17).Width = 60 : 清單1_dgv.Columns(18).Width = 65 : 清單1_dgv.Columns(19).Width = 60
        清單1_dgv.Columns(20).Width = 70 : 清單1_dgv.Columns(21).Width = 80 : 清單1_dgv.Columns(22).Width = 70

        清單1_dgv.Columns(23).Visible = False : 清單1_dgv.Columns(24).Visible = False

        清單1_dgv.Columns(25).Width = 168 : 清單1_dgv.Columns(26).Width = 60 : 清單1_dgv.Columns(27).Width = 60 : 清單1_dgv.Columns(28).Width = 80 : 清單1_dgv.Columns(29).Width = 100
        清單1_dgv.Columns(30).Width = 80 : 清單1_dgv.Columns(31).Width = 80 : 清單1_dgv.Columns(31).Width = 100 : 清單1_dgv.Columns(32).Width = 263 : 清單1_dgv.Columns(39).Width = 70
        清單1_dgv.Columns(40).Width = 85 : 清單1_dgv.Columns(41).Width = 57

        清單1_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 清單1_dgv.Columns(11).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(12).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(16).DefaultCellStyle.Format = "#,##0" : 清單1_dgv.Columns(18).DefaultCellStyle.Format = "#,##0.0000" : 清單1_dgv.Columns(19).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(20).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(21).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(22).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(26).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(27).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(28).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(29).DefaultCellStyle.Format = "#,##0" : 清單1_dgv.Columns(30).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(31).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(33).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(34).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(35).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(36).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(37).DefaultCellStyle.Format = "#,##0.00" : 清單1_dgv.Columns(38).DefaultCellStyle.Format = "#,##0.00"
        清單1_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(11).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(12).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(16).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(16).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(17).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(17).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(18).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(18).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(19).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(19).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(20).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(20).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(21).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(21).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(22).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(22).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(26).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(26).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(27).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(27).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(28).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(28).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(29).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(29).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(30).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(30).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(31).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(31).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(33).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(33).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(34).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(34).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(35).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(35).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(36).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(36).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(37).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(37).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(38).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(38).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(39).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(39).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(40).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(40).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單1_dgv.Columns(41).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單1_dgv.Columns(41).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        清單1_dgv.Columns(11).DefaultCellStyle.BackColor = Color.MistyRose : 清單1_dgv.Columns(16).DefaultCellStyle.BackColor = Color.MistyRose
        清單1_dgv.Columns(17).DefaultCellStyle.BackColor = Color.MistyRose : 清單1_dgv.Columns(18).DefaultCellStyle.BackColor = Color.MistyRose
        清單1_dgv.Columns(19).DefaultCellStyle.BackColor = Color.MistyRose : 清單1_dgv.Columns(21).DefaultCellStyle.BackColor = Color.MistyRose
        清單1_dgv.Columns(26).DefaultCellStyle.BackColor = Color.MistyRose : 清單1_dgv.Columns(27).DefaultCellStyle.BackColor = Color.MistyRose
        清單1_dgv.Columns(29).DefaultCellStyle.BackColor = Color.MistyRose

        清單1_dgv.Columns(0).DefaultCellStyle.ForeColor = Color.Blue : 清單1_dgv.Columns(1).DefaultCellStyle.ForeColor = Color.Blue
        清單1_dgv.Columns(2).DefaultCellStyle.ForeColor = Color.Blue : 清單1_dgv.Columns(3).DefaultCellStyle.ForeColor = Color.Blue
        清單1_dgv.Columns(4).DefaultCellStyle.ForeColor = Color.Blue : 清單1_dgv.Columns(5).DefaultCellStyle.ForeColor = Color.Blue
        清單1_dgv.Columns(6).DefaultCellStyle.ForeColor = Color.Blue : 清單1_dgv.Columns(7).DefaultCellStyle.ForeColor = Color.Blue
        清單1_dgv.Columns(8).DefaultCellStyle.ForeColor = Color.Blue : 清單1_dgv.Columns(9).DefaultCellStyle.ForeColor = Color.Blue
        清單1_dgv.Columns(10).DefaultCellStyle.ForeColor = Color.Blue

        清單1_dgv.Columns(12).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(20).DefaultCellStyle.ForeColor = Color.Red
        清單1_dgv.Columns(22).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(28).DefaultCellStyle.ForeColor = Color.Red
        清單1_dgv.Columns(30).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(31).DefaultCellStyle.ForeColor = Color.Red
        清單1_dgv.Columns(33).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(36).DefaultCellStyle.ForeColor = Color.Red
        清單1_dgv.Columns(34).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(37).DefaultCellStyle.ForeColor = Color.Red
        清單1_dgv.Columns(35).DefaultCellStyle.ForeColor = Color.Red : 清單1_dgv.Columns(38).DefaultCellStyle.ForeColor = Color.Red

        If BB(0) = True And BB(1) = False And BB(2) = False And BB(3) = False Then
            清單1_dgv.Columns(17).Visible = True : 清單1_dgv.Columns(18).Visible = True : 清單1_dgv.Columns(19).Visible = True : 清單1_dgv.Columns(20).Visible = True
            清單1_dgv.Columns(21).Visible = True : 清單1_dgv.Columns(22).Visible = True : 清單1_dgv.Columns(25).Visible = True : 清單1_dgv.Columns(13).Visible = True
            清單1_dgv.Columns(14).Visible = True : 清單1_dgv.Columns(16).Visible = True

            清單1_dgv.Columns(26).Visible = False : 清單1_dgv.Columns(27).Visible = False : 清單1_dgv.Columns(28).Visible = False : 清單1_dgv.Columns(29).Visible = False
            清單1_dgv.Columns(30).Visible = False : 清單1_dgv.Columns(31).Visible = False : 清單1_dgv.Columns(32).Visible = False : 清單1_dgv.Columns(33).Visible = False
            清單1_dgv.Columns(34).Visible = False : 清單1_dgv.Columns(35).Visible = False : 清單1_dgv.Columns(36).Visible = False : 清單1_dgv.Columns(37).Visible = False
            清單1_dgv.Columns(38).Visible = False : 清單1_dgv.Columns(39).Visible = False : 清單1_dgv.Columns(40).Visible = False : 清單1_dgv.Columns(41).Visible = False
        ElseIf BB(0) = False And BB(1) = True And BB(2) = False And BB(3) = False Then
            清單1_dgv.Columns(17).Visible = False : 清單1_dgv.Columns(18).Visible = False : 清單1_dgv.Columns(19).Visible = False : 清單1_dgv.Columns(20).Visible = False
            清單1_dgv.Columns(21).Visible = False : 清單1_dgv.Columns(22).Visible = False : 清單1_dgv.Columns(25).Visible = False : 清單1_dgv.Columns(13).Visible = False
            清單1_dgv.Columns(14).Visible = False : 清單1_dgv.Columns(16).Visible = False

            清單1_dgv.Columns(26).Visible = True : 清單1_dgv.Columns(27).Visible = True : 清單1_dgv.Columns(28).Visible = True : 清單1_dgv.Columns(29).Visible = True
            清單1_dgv.Columns(30).Visible = True : 清單1_dgv.Columns(31).Visible = True : 清單1_dgv.Columns(32).Visible = True : 清單1_dgv.Columns(39).Visible = True

            清單1_dgv.Columns(34).Visible = False : 清單1_dgv.Columns(35).Visible = False : 清單1_dgv.Columns(36).Visible = False : 清單1_dgv.Columns(37).Visible = False
            清單1_dgv.Columns(38).Visible = False : 清單1_dgv.Columns(33).Visible = False : 清單1_dgv.Columns(40).Visible = False : 清單1_dgv.Columns(41).Visible = False
        ElseIf BB(0) = False And BB(1) = False And BB(2) = True And BB(3) = False Then
            清單1_dgv.Columns(17).Visible = False : 清單1_dgv.Columns(18).Visible = False : 清單1_dgv.Columns(19).Visible = False : 清單1_dgv.Columns(20).Visible = False
            清單1_dgv.Columns(21).Visible = False : 清單1_dgv.Columns(22).Visible = False : 清單1_dgv.Columns(25).Visible = False : 清單1_dgv.Columns(13).Visible = False
            清單1_dgv.Columns(14).Visible = False : 清單1_dgv.Columns(16).Visible = False : 清單1_dgv.Columns(26).Visible = False : 清單1_dgv.Columns(27).Visible = False
            清單1_dgv.Columns(28).Visible = False : 清單1_dgv.Columns(29).Visible = False : 清單1_dgv.Columns(30).Visible = False : 清單1_dgv.Columns(31).Visible = False
            清單1_dgv.Columns(32).Visible = False

            清單1_dgv.Columns(33).Visible = True : 清單1_dgv.Columns(34).Visible = True : 清單1_dgv.Columns(35).Visible = True : 清單1_dgv.Columns(36).Visible = True
            清單1_dgv.Columns(37).Visible = True : 清單1_dgv.Columns(38).Visible = True : 清單1_dgv.Columns(39).Visible = True : 清單1_dgv.Columns(40).Visible = True
            清單1_dgv.Columns(41).Visible = True
        End If
        清單1_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
            清單1_dgv("年份", i).Value = Strings.Left(清單1_dgv("交期開始", i).Value.ToString, 4)
        Next
    End Sub
    Private Sub Set_grid3()
        清單2_dgv.Columns(0).ReadOnly = True : 清單2_dgv.Columns(1).ReadOnly = True : 清單2_dgv.Columns(2).ReadOnly = True : 清單2_dgv.Columns(3).ReadOnly = True
        清單2_dgv.Columns(4).ReadOnly = True : 清單2_dgv.Columns(5).ReadOnly = True : 清單2_dgv.Columns(6).ReadOnly = True : 清單2_dgv.Columns(7).ReadOnly = True
        清單2_dgv.Columns(8).ReadOnly = True : 清單2_dgv.Columns(9).ReadOnly = True : 清單2_dgv.Columns(10).ReadOnly = True : 清單2_dgv.Columns(12).ReadOnly = True
        清單2_dgv.Columns(13).ReadOnly = True : 清單2_dgv.Columns(14).ReadOnly = True : 清單2_dgv.Columns(20).ReadOnly = True : 清單2_dgv.Columns(22).ReadOnly = True
        清單2_dgv.Columns(28).ReadOnly = True : 清單2_dgv.Columns(30).ReadOnly = True : 清單2_dgv.Columns(31).ReadOnly = True : 清單2_dgv.Columns(33).ReadOnly = True
        清單2_dgv.Columns(34).ReadOnly = True : 清單2_dgv.Columns(35).ReadOnly = True : 清單2_dgv.Columns(36).ReadOnly = True : 清單2_dgv.Columns(37).ReadOnly = True
        清單2_dgv.Columns(38).ReadOnly = True : 清單2_dgv.Columns(39).ReadOnly = True : 清單2_dgv.Columns(40).ReadOnly = True
        '764
        清單2_dgv.Columns(0).Width = 90 : 清單2_dgv.Columns(1).Width = 100 : 清單2_dgv.Columns(2).Width = 115 : 清單2_dgv.Columns(3).Width = 80
        清單2_dgv.Columns(4).Width = 90 : 清單2_dgv.Columns(5).Width = 90 : 清單2_dgv.Columns(6).Width = 100 : 清單2_dgv.Columns(7).Width = 50
        清單2_dgv.Columns(8).Width = 60 : 清單2_dgv.Columns(9).Width = 70 : 清單2_dgv.Columns(10).Width = 70 : 清單2_dgv.Columns(11).Width = 60
        清單2_dgv.Columns(12).Width = 80 : 清單2_dgv.Columns(13).Width = 100 : 清單2_dgv.Columns(14).Width = 80 : 清單2_dgv.Columns(15).Visible = False
        清單2_dgv.Columns(16).Width = 60 : 清單2_dgv.Columns(17).Width = 60 : 清單2_dgv.Columns(18).Width = 65 : 清單2_dgv.Columns(19).Width = 60
        清單2_dgv.Columns(20).Width = 70

        清單2_dgv.Columns(21).Visible = False : 清單2_dgv.Columns(22).Visible = False : 清單2_dgv.Columns(23).Width = 80 : 清單2_dgv.Columns(24).Width = 105

        清單2_dgv.Columns(25).Width = 133 : 清單2_dgv.Columns(26).Width = 60 : 清單2_dgv.Columns(27).Width = 60 : 清單2_dgv.Columns(28).Width = 80 : 清單2_dgv.Columns(29).Width = 100
        清單2_dgv.Columns(30).Width = 80 : 清單2_dgv.Columns(31).Width = 80 : 清單2_dgv.Columns(31).Width = 100 : 清單2_dgv.Columns(32).Width = 263 : 清單2_dgv.Columns(39).Width = 70
        清單2_dgv.Columns(40).Width = 85 : 清單2_dgv.Columns(41).Width = 57

        清單2_dgv.Columns(8).DefaultCellStyle.Format = "#,##0" : 清單2_dgv.Columns(11).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(12).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(16).DefaultCellStyle.Format = "#,##0" : 清單2_dgv.Columns(18).DefaultCellStyle.Format = "#,##0.0000" : 清單2_dgv.Columns(19).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(20).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(21).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(22).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(26).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(27).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(28).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(29).DefaultCellStyle.Format = "#,##0" : 清單2_dgv.Columns(30).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(31).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(33).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(34).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(35).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(36).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(37).DefaultCellStyle.Format = "#,##0.00" : 清單2_dgv.Columns(38).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(23).DefaultCellStyle.Format = "#,##0.00"
        清單2_dgv.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(11).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(12).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(16).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(16).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(17).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(17).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(18).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(18).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(19).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(19).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(20).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(20).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(21).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(21).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(22).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(22).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(26).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(26).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(27).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(27).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(28).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(28).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(29).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(29).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(30).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(30).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(31).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(31).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(23).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(23).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(33).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(33).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(34).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(34).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(35).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(35).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(36).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(36).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(37).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(37).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(38).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(38).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(39).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(39).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(40).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(40).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        清單2_dgv.Columns(41).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : 清單2_dgv.Columns(41).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        清單2_dgv.Columns(11).DefaultCellStyle.BackColor = Color.MistyRose : 清單2_dgv.Columns(16).DefaultCellStyle.BackColor = Color.MistyRose
        清單2_dgv.Columns(17).DefaultCellStyle.BackColor = Color.MistyRose : 清單2_dgv.Columns(18).DefaultCellStyle.BackColor = Color.MistyRose
        清單2_dgv.Columns(19).DefaultCellStyle.BackColor = Color.MistyRose : 清單2_dgv.Columns(21).DefaultCellStyle.BackColor = Color.MistyRose
        清單2_dgv.Columns(26).DefaultCellStyle.BackColor = Color.MistyRose : 清單2_dgv.Columns(27).DefaultCellStyle.BackColor = Color.MistyRose
        清單2_dgv.Columns(29).DefaultCellStyle.BackColor = Color.MistyRose : 清單2_dgv.Columns(23).DefaultCellStyle.BackColor = Color.MistyRose
        清單2_dgv.Columns(24).DefaultCellStyle.BackColor = Color.MistyRose

        清單2_dgv.Columns(0).DefaultCellStyle.ForeColor = Color.Green : 清單2_dgv.Columns(1).DefaultCellStyle.ForeColor = Color.Green
        清單2_dgv.Columns(2).DefaultCellStyle.ForeColor = Color.Green : 清單2_dgv.Columns(3).DefaultCellStyle.ForeColor = Color.Green
        清單2_dgv.Columns(4).DefaultCellStyle.ForeColor = Color.Green : 清單2_dgv.Columns(5).DefaultCellStyle.ForeColor = Color.Green
        清單2_dgv.Columns(6).DefaultCellStyle.ForeColor = Color.Green : 清單2_dgv.Columns(7).DefaultCellStyle.ForeColor = Color.Green
        清單2_dgv.Columns(8).DefaultCellStyle.ForeColor = Color.Green : 清單2_dgv.Columns(9).DefaultCellStyle.ForeColor = Color.Green
        清單2_dgv.Columns(10).DefaultCellStyle.ForeColor = Color.Green

        清單2_dgv.Columns(12).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(20).DefaultCellStyle.ForeColor = Color.Red
        清單2_dgv.Columns(22).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(28).DefaultCellStyle.ForeColor = Color.Red
        清單2_dgv.Columns(30).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(31).DefaultCellStyle.ForeColor = Color.Red
        清單2_dgv.Columns(33).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(36).DefaultCellStyle.ForeColor = Color.Red
        清單2_dgv.Columns(34).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(37).DefaultCellStyle.ForeColor = Color.Red
        清單2_dgv.Columns(35).DefaultCellStyle.ForeColor = Color.Red : 清單2_dgv.Columns(38).DefaultCellStyle.ForeColor = Color.Red

        If BB(0) = True And BB(1) = False And BB(2) = False And BB(3) = False Then
            清單2_dgv.Columns(17).Visible = True : 清單2_dgv.Columns(18).Visible = True : 清單2_dgv.Columns(19).Visible = True : 清單2_dgv.Columns(20).Visible = True
            清單2_dgv.Columns(23).Visible = True : 清單2_dgv.Columns(24).Visible = True : 清單2_dgv.Columns(25).Visible = True : 清單2_dgv.Columns(13).Visible = True
            清單2_dgv.Columns(14).Visible = True : 清單2_dgv.Columns(16).Visible = True

            清單2_dgv.Columns(26).Visible = False : 清單2_dgv.Columns(27).Visible = False : 清單2_dgv.Columns(28).Visible = False : 清單2_dgv.Columns(29).Visible = False
            清單2_dgv.Columns(30).Visible = False : 清單2_dgv.Columns(31).Visible = False : 清單2_dgv.Columns(32).Visible = False : 清單2_dgv.Columns(33).Visible = False
            清單2_dgv.Columns(34).Visible = False : 清單2_dgv.Columns(35).Visible = False : 清單2_dgv.Columns(36).Visible = False : 清單2_dgv.Columns(37).Visible = False
            清單2_dgv.Columns(38).Visible = False : 清單2_dgv.Columns(39).Visible = False : 清單2_dgv.Columns(40).Visible = False : 清單2_dgv.Columns(41).Visible = False
        ElseIf BB(0) = False And BB(1) = True And BB(2) = False And BB(3) = False Then
            清單2_dgv.Columns(17).Visible = False : 清單2_dgv.Columns(18).Visible = False : 清單2_dgv.Columns(19).Visible = False : 清單2_dgv.Columns(20).Visible = False
            清單2_dgv.Columns(23).Visible = False : 清單2_dgv.Columns(24).Visible = False : 清單2_dgv.Columns(25).Visible = False : 清單2_dgv.Columns(13).Visible = False
            清單2_dgv.Columns(14).Visible = False : 清單2_dgv.Columns(16).Visible = False

            清單2_dgv.Columns(26).Visible = True : 清單2_dgv.Columns(27).Visible = True : 清單2_dgv.Columns(28).Visible = True : 清單2_dgv.Columns(29).Visible = True
            清單2_dgv.Columns(30).Visible = True : 清單2_dgv.Columns(31).Visible = True : 清單2_dgv.Columns(32).Visible = True : 清單2_dgv.Columns(39).Visible = True

            清單2_dgv.Columns(34).Visible = False : 清單2_dgv.Columns(35).Visible = False : 清單2_dgv.Columns(36).Visible = False : 清單2_dgv.Columns(37).Visible = False
            清單2_dgv.Columns(38).Visible = False : 清單2_dgv.Columns(33).Visible = False : 清單2_dgv.Columns(40).Visible = False : 清單2_dgv.Columns(41).Visible = False
        ElseIf BB(0) = False And BB(1) = False And BB(2) = True And BB(3) = False Then
            清單2_dgv.Columns(17).Visible = False : 清單2_dgv.Columns(18).Visible = False : 清單2_dgv.Columns(19).Visible = False : 清單2_dgv.Columns(20).Visible = False
            清單2_dgv.Columns(23).Visible = False : 清單2_dgv.Columns(24).Visible = False : 清單2_dgv.Columns(25).Visible = False : 清單2_dgv.Columns(13).Visible = False
            清單2_dgv.Columns(14).Visible = False : 清單2_dgv.Columns(16).Visible = False : 清單2_dgv.Columns(26).Visible = False : 清單2_dgv.Columns(27).Visible = False
            清單2_dgv.Columns(28).Visible = False : 清單2_dgv.Columns(29).Visible = False : 清單2_dgv.Columns(30).Visible = False : 清單2_dgv.Columns(31).Visible = False
            清單2_dgv.Columns(32).Visible = False

            清單2_dgv.Columns(33).Visible = True : 清單2_dgv.Columns(34).Visible = True : 清單2_dgv.Columns(35).Visible = True : 清單2_dgv.Columns(36).Visible = True
            清單2_dgv.Columns(37).Visible = True : 清單2_dgv.Columns(38).Visible = True : 清單2_dgv.Columns(39).Visible = True : 清單2_dgv.Columns(40).Visible = True
            清單2_dgv.Columns(41).Visible = True
        End If
        清單2_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
            清單2_dgv("年份", i).Value = Strings.Left(清單2_dgv("交期開始", i).Value.ToString, 4)
        Next
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).FillWeight = 100
    End Sub
    Private Sub Set_自動彙總1()
        TextBox2.Text = "0" : TextBox19.Text = "0" : TextBox34.Text = "0" : TextBox3.Text = "0" : TextBox4.Text = "0" : TextBox36.Text = "0" : TextBox5.Text = "0" : TextBox11.Text = "0"
        TextBox35.Text = "0" : TextBox9.Text = "0" : TextBox20.Text = "0" : TextBox33.Text = "0" : TextBox21.Text = "0" : TextBox22.Text = "0" : TextBox32.Text = "0" : TextBox24.Text = "0"
        TextBox23.Text = "0" : TextBox31.Text = "0" : TextBox26.Text = "0" : TextBox25.Text = "0" : TextBox30.Text = "0" : TextBox28.Text = "0" : TextBox27.Text = "0" : TextBox29.Text = "0"
        For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
            If 清單1_dgv("Discount price", i).Value.ToString = "" Then : 清單1_dgv("Discount price", i).Value = 0 : End If
            If 清單1_dgv("Commercial invoice", i).Value.ToString = "" Then : 清單1_dgv("Commercial invoice", i).Value = 0 : End If
            If 清單1_dgv("Mold cost", i).Value.ToString = "" Then : 清單1_dgv("Mold cost", i).Value = 0 : End If
            If 清單1_dgv("Fty price", i).Value.ToString = "" Then : 清單1_dgv("Fty price", i).Value = 0 : End If

            清單1_dgv("銷售金額", i).Value = Double.Parse(清單1_dgv("單價", i).Value.ToString) * Double.Parse(清單1_dgv("訂單數量", i).Value)
            清單1_dgv("Discount price", i).Value = Double.Parse(清單1_dgv("單價", i).Value.ToString)
            清單1_dgv("Overage invoice", i).Value = Double.Parse(清單1_dgv("Discount price", i).Value.ToString) - Double.Parse(清單1_dgv("Commercial invoice", i).Value)
            清單1_dgv("Collected mold cost", i).Value = Double.Parse(清單1_dgv("Mold cost", i).Value.ToString) * Double.Parse(清單1_dgv("訂單數量", i).Value)
            If Double.Parse(清單1_dgv("Discount price", i).Value.ToString) - Double.Parse(清單1_dgv("Mold cost", i).Value) = 0 Then
                清單1_dgv("Profit", i).Value = 0
            Else
                清單1_dgv("Profit", i).Value = (Double.Parse(清單1_dgv("Discount price", i).Value.ToString) - Double.Parse(清單1_dgv("Mold cost", i).Value) -
                                    Double.Parse(清單1_dgv("Fty price", i).Value)) / (Double.Parse(清單1_dgv("Discount price", i).Value.ToString) -
                                    Double.Parse(清單1_dgv("Mold cost", i).Value)) * 100
            End If
            清單1_dgv("Commission", i).Value = (Double.Parse(清單1_dgv("單價", i).Value.ToString) - Double.Parse(清單1_dgv("Fty price", i).Value) -
                                                        Double.Parse(清單1_dgv("Mold cost", i).Value)) * Double.Parse(清單1_dgv("訂單數量", i).Value)
            清單1_dgv("客戶付款", i).Value = Double.Parse(清單1_dgv("Commercial invoice", i).Value.ToString) * Double.Parse(清單1_dgv("訂單數量", i).Value)
            清單1_dgv("未收款項", i).Value = Double.Parse(清單1_dgv("Overage invoice", i).Value.ToString) * Double.Parse(清單1_dgv("訂單數量", i).Value)
            清單1_dgv("預收款項", i).Value = 0
            清單1_dgv("成本金額", i).Value = (Double.Parse(清單1_dgv("Fty price", i).Value.ToString) + Double.Parse(清單1_dgv("Mold cost", i).Value)) *
                                                          Double.Parse(清單1_dgv("訂單數量", i).Value)
            清單1_dgv("利潤", i).Value = (Double.Parse(清單1_dgv("銷售金額", i).Value.ToString) - Double.Parse(清單1_dgv("成本金額", i).Value))
            清單1_dgv("實際收款", i).Value = (Double.Parse(清單1_dgv("利潤", i).Value.ToString) - Double.Parse(清單1_dgv("未收款項", i).Value))
            TextBox2.Text = Val(TextBox2.Text) + Double.Parse(清單1_dgv("訂單數量", i).Value) : TextBox3.Text = Val(TextBox3.Text) + Double.Parse(清單1_dgv("銷售金額", i).Value)
            TextBox5.Text = Val(TextBox5.Text) + Double.Parse(清單1_dgv("客戶付款", i).Value) : TextBox9.Text = Val(TextBox9.Text) + Double.Parse(清單1_dgv("未收款項", i).Value)
            TextBox21.Text = Val(TextBox21.Text) + Double.Parse(清單1_dgv("預收款項", i).Value) : TextBox24.Text = Val(TextBox24.Text) + Double.Parse(清單1_dgv("成本金額", i).Value)
            TextBox26.Text = Val(TextBox26.Text) + Double.Parse(清單1_dgv("利潤", i).Value) : TextBox28.Text = Val(TextBox28.Text) + Double.Parse(清單1_dgv("實際收款", i).Value)
        Next

        For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
            If 清單2_dgv("Discount price", i).Value.ToString = "" Then : 清單2_dgv("Discount price", i).Value = 0 : End If
            If 清單2_dgv("Commercial invoice", i).Value.ToString = "" Then : 清單2_dgv("Commercial invoice", i).Value = 0 : End If
            If 清單2_dgv("Mold cost", i).Value.ToString = "" Then : 清單2_dgv("Mold cost", i).Value = 0 : End If
            If 清單2_dgv("Fty price", i).Value.ToString = "" Then : 清單2_dgv("Fty price", i).Value = 0 : End If
            If 清單2_dgv("Selling price", i).Value.ToString = "" Then : 清單2_dgv("Selling price", i).Value = 0 : End If

            清單2_dgv("銷售金額", i).Value = Double.Parse(清單2_dgv("單價", i).Value.ToString) * Double.Parse(清單2_dgv("訂單數量", i).Value)
            清單2_dgv("Discount price", i).Value = Double.Parse(清單2_dgv("單價", i).Value.ToString)
            清單2_dgv("Overage invoice", i).Value = Double.Parse(清單2_dgv("Discount price", i).Value.ToString) - Double.Parse(清單2_dgv("Commercial invoice", i).Value)
            清單2_dgv("Collected mold cost", i).Value = Double.Parse(清單2_dgv("Mold cost", i).Value.ToString) * Double.Parse(清單2_dgv("訂單數量", i).Value)
            If Double.Parse(清單2_dgv("Discount price", i).Value.ToString) - Double.Parse(清單2_dgv("Mold cost", i).Value) = 0 Then
                清單2_dgv("Profit", i).Value = 0
            Else
                清單2_dgv("Profit", i).Value = (Double.Parse(清單2_dgv("Discount price", i).Value.ToString) - Double.Parse(清單2_dgv("Mold cost", i).Value) -
                                Double.Parse(清單2_dgv("Fty price", i).Value)) / (Double.Parse(清單2_dgv("Discount price", i).Value.ToString) -
                                Double.Parse(清單2_dgv("Mold cost", i).Value)) * 100
            End If
            清單2_dgv("Commission", i).Value = (Double.Parse(清單2_dgv("單價", i).Value.ToString) - Double.Parse(清單2_dgv("Fty price", i).Value) -
                                    Double.Parse(清單2_dgv("Mold cost", i).Value)) * Double.Parse(清單2_dgv("訂單數量", i).Value)
            清單2_dgv("客戶付款", i).Value = Double.Parse(清單2_dgv("Selling price", i).Value.ToString) * Double.Parse(清單2_dgv("訂單數量", i).Value)
            清單2_dgv("未收款項", i).Value = 0
            If 清單2_dgv("Selling price", i).Value = 0 Then : 清單2_dgv("預收款項", i).Value = 0 : Else
                清單2_dgv("預收款項", i).Value = (Double.Parse(清單2_dgv("Selling price", i).Value.ToString) - Double.Parse(清單2_dgv("Discount price", i).Value)) *
                                                      Double.Parse(清單2_dgv("訂單數量", i).Value)
            End If
            清單2_dgv("成本金額", i).Value = (Double.Parse(清單2_dgv("Fty price", i).Value.ToString) + Double.Parse(清單2_dgv("Mold cost", i).Value)) *
                                      Double.Parse(清單2_dgv("訂單數量", i).Value)
            清單2_dgv("利潤", i).Value = (Double.Parse(清單2_dgv("銷售金額", i).Value.ToString) - Double.Parse(清單2_dgv("成本金額", i).Value))
            清單2_dgv("實際收款", i).Value = (Double.Parse(清單2_dgv("利潤", i).Value.ToString) + Double.Parse(清單2_dgv("預收款項", i).Value))
            TextBox19.Text = Val(TextBox19.Text) + Double.Parse(清單2_dgv("訂單數量", i).Value) : TextBox4.Text = Val(TextBox4.Text) + Double.Parse(清單2_dgv("銷售金額", i).Value)
            TextBox11.Text = Val(TextBox11.Text) + Double.Parse(清單2_dgv("客戶付款", i).Value) : TextBox20.Text = Val(TextBox20.Text) + Double.Parse(清單2_dgv("未收款項", i).Value)
            TextBox22.Text = Val(TextBox22.Text) + Double.Parse(清單2_dgv("預收款項", i).Value) : TextBox23.Text = Val(TextBox23.Text) + Double.Parse(清單2_dgv("成本金額", i).Value)
            TextBox25.Text = Val(TextBox25.Text) + Double.Parse(清單2_dgv("利潤", i).Value) : TextBox27.Text = Val(TextBox27.Text) + Double.Parse(清單2_dgv("實際收款", i).Value)
        Next
        TextBox34.Text = Val(TextBox19.Text) + Val(TextBox2.Text) : TextBox36.Text = Val(TextBox3.Text) + Val(TextBox4.Text) : TextBox35.Text = Val(TextBox5.Text) + Val(TextBox11.Text)
        TextBox33.Text = Val(TextBox9.Text) + Val(TextBox20.Text) : TextBox32.Text = Val(TextBox21.Text) + Val(TextBox22.Text) : TextBox31.Text = Val(TextBox24.Text) + Val(TextBox23.Text)
        TextBox30.Text = Val(TextBox26.Text) + Val(TextBox25.Text) : TextBox29.Text = Val(TextBox28.Text) + Val(TextBox27.Text) : TextBox37.Text = Val(TextBox32.Text) - Val(TextBox33.Text)

        If Val(TextBox28.Text) >= 0 Then : TextBox28.ForeColor = Color.Blue : Else : TextBox28.ForeColor = Color.Red : End If
        If Val(TextBox27.Text) >= 0 Then : TextBox27.ForeColor = Color.Blue : Else : TextBox27.ForeColor = Color.Red : End If
        If Val(TextBox29.Text) >= 0 Then : TextBox29.ForeColor = Color.Blue : Else : TextBox29.ForeColor = Color.Red : End If
        If Val(TextBox37.Text) >= 0 Then : TextBox37.ForeColor = Color.Blue : Else : TextBox37.ForeColor = Color.Red : End If

        TextBox2.Text = Strings.Format(Val(TextBox2.Text), "#,##0") : TextBox19.Text = Strings.Format(Val(TextBox19.Text), "#,##0") : TextBox34.Text = Strings.Format(Val(TextBox34.Text), "#,##0")
        TextBox3.Text = Strings.Format(Val(TextBox3.Text), "#,##0.00") : TextBox4.Text = Strings.Format(Val(TextBox4.Text), "#,##0.00") : TextBox36.Text = Strings.Format(Val(TextBox36.Text), "#,##0.00")
        TextBox5.Text = Strings.Format(Val(TextBox5.Text), "#,##0.00") : TextBox11.Text = Strings.Format(Val(TextBox11.Text), "#,##0.00") : TextBox35.Text = Strings.Format(Val(TextBox35.Text), "#,##0.00")
        TextBox9.Text = Strings.Format(Val(TextBox9.Text), "#,##0.00") : TextBox20.Text = Strings.Format(Val(TextBox20.Text), "#,##0.00") : TextBox33.Text = Strings.Format(Val(TextBox33.Text), "#,##0.00")
        TextBox21.Text = Strings.Format(Val(TextBox21.Text), "#,##0.00") : TextBox22.Text = Strings.Format(Val(TextBox22.Text), "#,##0.00") : TextBox32.Text = Strings.Format(Val(TextBox32.Text), "#,##0.00")
        TextBox24.Text = Strings.Format(Val(TextBox24.Text), "#,##0.00") : TextBox23.Text = Strings.Format(Val(TextBox23.Text), "#,##0.00") : TextBox31.Text = Strings.Format(Val(TextBox31.Text), "#,##0.00")
        TextBox26.Text = Strings.Format(Val(TextBox26.Text), "#,##0.00") : TextBox25.Text = Strings.Format(Val(TextBox25.Text), "#,##0.00") : TextBox30.Text = Strings.Format(Val(TextBox30.Text), "#,##0.00")
        TextBox28.Text = Strings.Format(Val(TextBox28.Text), "#,##0.00") : TextBox27.Text = Strings.Format(Val(TextBox27.Text), "#,##0.00") : TextBox29.Text = Strings.Format(Val(TextBox29.Text), "#,##0.00")
        TextBox37.Text = Strings.Format(Val(TextBox37.Text), "#,##0.00")
    End Sub
    Private Sub Set_格式()
        TextBox1.Enabled = False : TextBox12.Enabled = False : TextBox13.Enabled = False : TextBox14.Enabled = False : TextBox15.Enabled = False
        TextBox6.Visible = False : TextBox7.Visible = False : TextBox16.Visible = False : TextBox17.Visible = False : TextBox10.Visible = False : TextBox8.Visible = False
        Button6.Enabled = False : Timer1.Enabled = False : Label47.Visible = False : Button5.Enabled = True : Button9.Enabled = True
        Button11.Enabled = False : Button12.Enabled = False : Button13.Enabled = False
    End Sub
    Private Sub 訂單價格利潤明細表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        ComboBox1.Text = Year(Today) : ComboBox10.SelectedIndex = 0 : ComboBox2.SelectedIndex = 0
        BB(0) = True : BB(1) = False : BB(2) = False : BB(3) = False
        Button34.Enabled = False : TextBox18.Visible = False
        Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_未建檔清單() : Set_grid1() : Set_自動彙總1() : Set_格式()
    End Sub
    Private Sub ComboBox10_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox10.TextChanged
        If ComboBox10.Text = "LANDED BRANDED" Then
            清單1_dgv.Visible = True : 清單2_dgv.Visible = False
        Else
            清單1_dgv.Visible = False : 清單2_dgv.Visible = True
        End If
        Set_未建檔清單() : Set_自動彙總1()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Label47.Visible = True Then : Label47.Visible = False : Else : Label47.Visible = True : End If
    End Sub
    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 清單1_dgv.CellEndEdit
        Set_自動彙總1()
    End Sub
    Private Sub DataGridView3_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 清單2_dgv.CellEndEdit
        Set_自動彙總1()
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 清單1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 清單1_dgv("訂單流水號", e.RowIndex).Value.ToString : TextBox12.Text = 清單1_dgv("客戶訂單號", e.RowIndex).Value.ToString
            TextBox13.Text = 清單1_dgv("型體號碼", e.RowIndex).Value.ToString : TextBox14.Text = 清單1_dgv("顏色", e.RowIndex).Value.ToString
            TextBox15.Text = 清單1_dgv("楦型", e.RowIndex).Value.ToString : TextBox16.Text = 清單1_dgv("資料流水號", e.RowIndex).Value.ToString
            ComboBox1.Text = 清單1_dgv("年份", e.RowIndex).Value.ToString : ComboBox2.Text = 清單1_dgv("Currency", e.RowIndex).Value.ToString
            TextBox17.Text = 清單1_dgv("利潤表流水號", e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub DataGridView3_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 清單2_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = 清單2_dgv("訂單流水號", e.RowIndex).Value.ToString : TextBox12.Text = 清單2_dgv("客戶訂單號", e.RowIndex).Value.ToString
            TextBox13.Text = 清單2_dgv("型體號碼", e.RowIndex).Value.ToString : TextBox14.Text = 清單2_dgv("顏色", e.RowIndex).Value.ToString
            TextBox15.Text = 清單2_dgv("楦型", e.RowIndex).Value.ToString : TextBox16.Text = 清單2_dgv("資料流水號", e.RowIndex).Value.ToString
            ComboBox1.Text = 清單2_dgv("年份", e.RowIndex).Value.ToString : ComboBox2.Text = 清單2_dgv("Currency", e.RowIndex).Value.ToString
            TextBox17.Text = 清單2_dgv("利潤表流水號", e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView2(0, e.RowIndex).Value.ToString
            Timer1.Enabled = True : Button6.Enabled = True : CheckBox1.Checked = True : CheckBox2.Checked = False : Button5.Enabled = False : Button9.Enabled = False
            Button11.Enabled = True : Button12.Enabled = True : Button13.Enabled = True
            Set_訂單清單1() : Set_訂單清單2()
        End If
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        If CheckBox1.Checked = True Then
            If TextBox16.Text = "" Or TextBox1.Text = "" Then
                MsgBox("沒有選擇將要拆分的資料")
            Else
                Dim NUM2 As Integer
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "Select Top(1) 利潤表流水號 FROM 訂單價格利潤表暫存檔 ORDER BY 利潤表流水號 DESC"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then : NUM2 = Strings.Right(dr("利潤表流水號").ToString, 8) : End If
                conn.Close()
                NUM2 += 1
                If NUM2 < 10 Then : WW = "PR" & "0000000" & NUM2
                ElseIf NUM2 > 9 And NUM2 < 100 Then : WW = "PR" & "000000" & NUM2
                ElseIf NUM2 > 99 And NUM2 < 1000 Then : WW = "PR" & "00000" & NUM2
                ElseIf NUM2 > 999 And NUM2 < 10000 Then : WW = "PR" & "0000" & NUM2
                ElseIf NUM2 > 9999 And NUM2 < 100000 Then : WW = "PR" & "000" & NUM2
                ElseIf NUM2 > 99999 And NUM2 < 100000 Then : WW = "PR" & "00" & NUM2
                ElseIf NUM2 > 999999 And NUM2 < 1000000 Then : WW = "PR" & "0" & NUM2
                ElseIf NUM2 > 9999999 Then : WW = "PR" & NUM2
                End If
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 訂單價格利潤表暫存檔 (資料流水號, 利潤表流水號, 訂單流水號) " &
                           "VALUES ('" & TextBox16.Text & "','" & WW & "', '" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Set_編輯清單1() : Set_編輯清單2() : Set_grid() : Set_grid3() : Set_自動彙總1()
            End If
        ElseIf CheckBox1.Checked = False Then
            Dim NUM2 As Integer
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP (1) 利潤表流水號 FROM 訂單價格利潤明細表 ORDER BY 利潤表流水號 DESC"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : NUM2 = Strings.Right(dr("利潤表流水號").ToString, 8) : End If
            conn.Close()
            NUM2 += 1
            If NUM2 < 10 Then : WW = "PR" & "0000000" & NUM2
            ElseIf NUM2 > 9 And NUM2 < 100 Then : WW = "PR" & "000000" & NUM2
            ElseIf NUM2 > 99 And NUM2 < 1000 Then : WW = "PR" & "00000" & NUM2
            ElseIf NUM2 > 999 And NUM2 < 10000 Then : WW = "PR" & "0000" & NUM2
            ElseIf NUM2 > 9999 And NUM2 < 100000 Then : WW = "PR" & "000" & NUM2
            ElseIf NUM2 > 99999 And NUM2 < 100000 Then : WW = "PR" & "00" & NUM2
            ElseIf NUM2 > 999999 And NUM2 < 1000000 Then : WW = "PR" & "0" & NUM2
            ElseIf NUM2 > 9999999 Then : WW = "PR" & NUM2
            End If
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 訂單價格利潤明細表 (資料流水號, 訂單流水號, 客戶訂單號, 利潤表流水號, 年份, Quantity, Exchange, 審核, 類型, Price, 
                            Discount_price, Commercial_invoice, Overage_invoice, Selling_price, HTS_code, Remark1, Fty_price, Mold_cost, 
                            Collected_mold_cost, Amortization_qty, Profit, Commission, Remark2, Currency) " &
               "VALUES ('" & TextBox16.Text & "', '" & TextBox1.Text & "', '" & TextBox12.Text & "', '" & WW & "', '" & ComboBox1.Text & "', N'0', N'0', N'0', N'0', '" & ComboBox10.Text &
                        "', N'0', N'0', N'0', N'0', N'0', N'', N'', N'0', N'0', N'0', N'0', N'0', N'0', N'', '" & ComboBox2.Text & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_自動彙總1()
        End If
    End Sub
    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        If CheckBox1.Checked = True Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 訂單價格利潤表暫存檔 WHERE 利潤表流水號 LIKE '" & TextBox17.Text & "'"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : conn.Close()
            Set_編輯清單1() : Set_編輯清單2() : Set_grid() : Set_grid3() : Set_自動彙總1()
        ElseIf CheckBox1.Checked = False Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 訂單價格利潤明細表 WHERE 利潤表流水號 LIKE '" & TextBox17.Text & "'"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : conn.Close()
            Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_自動彙總1()
        End If
    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 清單1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 清單1_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 清單1_dgv(2, e.RowIndex).Value.ToString <> 清單1_dgv(2, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(清單1_dgv.RowHeadersVisible, 清單1_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 清單1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 清單1_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub DataGridView3_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 清單2_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 清單2_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 清單2_dgv(2, e.RowIndex).Value.ToString <> 清單2_dgv(2, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(清單2_dgv.RowHeadersVisible, 清單2_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 清單2_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 清單2_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PA = ""
        CheckBox1.Checked = False : CheckBox2.Checked = True : TextBox1.Text = ""
        Timer1.Enabled = False : Label47.Visible = False : Button6.Enabled = False : Button5.Enabled = True : Button9.Enabled = True
        Button11.Enabled = False : Button12.Enabled = False : Button13.Enabled = False
        Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_未建檔清單() : Set_grid1() : Set_自動彙總1()
    End Sub
    Private Sub Button34_Click(sender As Object, e As EventArgs) Handles Button34.Click
        Button34.Enabled = False : Button35.Enabled = True : Button2.Enabled = True
        BB(0) = True : BB(1) = False : BB(2) = False : BB(3) = False
        Set_grid() : Set_grid3()
    End Sub
    Private Sub Button35_Click(sender As Object, e As EventArgs) Handles Button35.Click
        Button34.Enabled = True : Button35.Enabled = False : Button2.Enabled = True
        BB(0) = False : BB(1) = True : BB(2) = False : BB(3) = False
        Set_grid() : Set_grid3()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Button34.Enabled = True : Button35.Enabled = True : Button2.Enabled = False
        BB(0) = False : BB(1) = False : BB(2) = True : BB(3) = False
        Set_grid() : Set_grid3()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        PA = InputBox("請輸入要查詢的資料") : TextBox1.Text = ""
        If ComboBox10.Text = "LANDED BRANDED" Then
            Set_訂單清單1() : Set_grid() : Set_自動彙總1()
        Else
            Set_訂單清單2() : Set_grid3() : Set_自動彙總1()
        End If
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        CheckBox1.Checked = True : CheckBox2.Checked = False
    End Sub
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.Click
        CheckBox1.Checked = False : CheckBox2.Checked = True
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Set_未建檔清單() : Set_grid1()
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If ComboBox1.Text = "" Or ComboBox2.Text = "" Then
            MsgBox("年份或幣別沒有選擇")
        Else
            If ComboBox10.Text = "LANDED BRANDED" Then
                For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
                    資料數 = 清單1_dgv.Rows.Count : 進度條()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT 利潤表流水號 FROM 訂單價格利潤明細表 WHERE (資料流水號 LIKE '" & 清單1_dgv.Rows(i).Cells("資料流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    If dr.Read() Then
                        conn.Close()
                    Else
                        conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "UPDATE 訂單控制表 SET  單價 = '" & 清單1_dgv.Rows(i).Cells("單價").Value & "', 總金額 = '" & 清單1_dgv.Rows(i).Cells("銷售金額").Value & "' " &
                       "WHERE (資料流水號 LIKE '" & 清單1_dgv.Rows(i).Cells("資料流水號").Value & "')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If

                        SQL1 = "INSERT INTO 訂單價格利潤明細表 (資料流水號, 訂單流水號, 客戶訂單號, 利潤表流水號, 年份, Quantity, Exchange, 審核, 類型, Price, 
                            Discount_price, Commercial_invoice, Overage_invoice, Selling_price, HTS_code, Remark1, Fty_price, Mold_cost, 
                            Collected_mold_cost, Amortization_qty, Profit, Commission, Remark2, Currency) " &
                       "VALUES ('" & 清單1_dgv.Rows(i).Cells("資料流水號").Value & "', '" & 清單1_dgv.Rows(i).Cells("訂單流水號").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("客戶訂單號").Value & "', '" & 清單1_dgv.Rows(i).Cells("利潤表流水號").Value &
                            "', '" & ComboBox1.Text & "', '" & 清單1_dgv.Rows(i).Cells("Quantity").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Exchange").Value & "', '" & 清單1_dgv.Rows(i).Cells("審核").Value & "', '" & ComboBox10.Text &
                            "', '" & 清單1_dgv.Rows(i).Cells("Price").Value & "', '" & 清單1_dgv.Rows(i).Cells("Discount price").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Commercial invoice").Value & "', '" & 清單1_dgv.Rows(i).Cells("Overage invoice").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Selling price").Value & "', '" & 清單1_dgv.Rows(i).Cells("HTS code").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Remark1").Value & "', '" & 清單1_dgv.Rows(i).Cells("Fty price").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Mold cost").Value & "', '" & 清單1_dgv.Rows(i).Cells("Collected mold cost").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Amortization qty").Value & "', '" & 清單1_dgv.Rows(i).Cells("Profit").Value &
                            "', '" & 清單1_dgv.Rows(i).Cells("Commission").Value & "', '" & 清單1_dgv.Rows(i).Cells("Remark2").Value & "', '" & ComboBox2.Text & "')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    End If
                Next i : ProgressBar1.Value = 0 : 最終數 = 0
            Else
                For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
                    資料數 = 清單2_dgv.Rows.Count : 進度條()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT 利潤表流水號 FROM 訂單價格利潤明細表 WHERE (資料流水號 LIKE '" & 清單2_dgv.Rows(i).Cells("資料流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    If dr.Read() Then
                        conn.Close()
                    Else
                        conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "UPDATE 訂單控制表 SET  單價 = '" & 清單2_dgv.Rows(i).Cells("單價").Value & "', 總金額 = '" & 清單2_dgv.Rows(i).Cells("銷售金額").Value & "' " &
                       "WHERE (資料流水號 LIKE '" & 清單2_dgv.Rows(i).Cells("資料流水號").Value & "')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If

                        SQL1 = "INSERT INTO 訂單價格利潤明細表 (資料流水號, 訂單流水號, 客戶訂單號, 利潤表流水號, 年份, Quantity, Exchange, 審核, 類型, Price, 
                            Discount_price, Commercial_invoice, Overage_invoice, Selling_price, HTS_code, Remark1, Fty_price, Mold_cost, 
                            Collected_mold_cost, Amortization_qty, Profit, Commission, Remark2, Currency) " &
                       "VALUES ('" & 清單2_dgv.Rows(i).Cells("資料流水號").Value & "', '" & 清單2_dgv.Rows(i).Cells("訂單流水號").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("客戶訂單號").Value & "', '" & 清單2_dgv.Rows(i).Cells("利潤表流水號").Value &
                            "', '" & ComboBox1.Text & "', '" & 清單2_dgv.Rows(i).Cells("Quantity").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Exchange").Value & "', '" & 清單2_dgv.Rows(i).Cells("審核").Value & "', '" & ComboBox10.Text &
                            "', '" & 清單2_dgv.Rows(i).Cells("Price").Value & "', '" & 清單2_dgv.Rows(i).Cells("Discount price").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Commercial invoice").Value & "', '" & 清單2_dgv.Rows(i).Cells("Overage invoice").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Selling price").Value & "', '" & 清單2_dgv.Rows(i).Cells("HTS code").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Remark1").Value & "', '" & 清單2_dgv.Rows(i).Cells("Fty price").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Mold cost").Value & "', '" & 清單2_dgv.Rows(i).Cells("Collected mold cost").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Amortization qty").Value & "', '" & 清單2_dgv.Rows(i).Cells("Profit").Value &
                            "', '" & 清單2_dgv.Rows(i).Cells("Commission").Value & "', '" & 清單2_dgv.Rows(i).Cells("Remark2").Value & "', '" & ComboBox2.Text & "')"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    End If
                Next i : ProgressBar1.Value = 0 : 最終數 = 0
            End If
            MsgBox("存檔完成")
            CheckBox1.Checked = False : CheckBox2.Checked = True
            Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_未建檔清單() : Set_grid1() : Set_自動彙總1() : Set_格式()
        End If
    End Sub
    Private Sub 進度條()
        平均數 = 10000 / 資料數
        最終數 += 平均數
        If 最終數 > 10000 Then : 最終數 = 10000 : End If
        ProgressBar1.Value = 最終數
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        If ComboBox1.Text = "" Or ComboBox2.Text = "" Then
            MsgBox("年份或幣別沒有選擇")
        Else
            If ComboBox10.Text = "LANDED BRANDED" Then
                For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
                    資料數 = 清單1_dgv.Rows.Count : 進度條()
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單控制表 SET  單價 = '" & 清單1_dgv.Rows(i).Cells("單價").Value & "', 總金額 = '" & 清單1_dgv.Rows(i).Cells("銷售金額").Value & "' " &
                           "WHERE (資料流水號 LIKE '" & 清單1_dgv.Rows(i).Cells("資料流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單價格利潤明細表 SET  年份 = '" & 清單1_dgv.Rows(i).Cells("年份").Value & "', Quantity = '" & 清單1_dgv.Rows(i).Cells("Quantity").Value &
                                                    "', Exchange = '" & 清單1_dgv.Rows(i).Cells("Exchange").Value & "', 審核 = '" & 清單1_dgv.Rows(i).Cells("審核").Value &
                                                    "', Price = '" & 清單1_dgv.Rows(i).Cells("Price").Value & "', Discount_price = '" & 清單1_dgv.Rows(i).Cells("Discount price").Value &
                                                    "', Commercial_invoice = '" & 清單1_dgv.Rows(i).Cells("Commercial invoice").Value &
                                                    "', Overage_invoice = '" & 清單1_dgv.Rows(i).Cells("Overage invoice").Value &
                                                    "', Selling_price = '" & 清單1_dgv.Rows(i).Cells("Selling price").Value & "', HTS_code = '" & 清單1_dgv.Rows(i).Cells("HTS code").Value &
                                                    "', Remark1 = '" & 清單1_dgv.Rows(i).Cells("Remark1").Value & "', Fty_price = '" & 清單1_dgv.Rows(i).Cells("Fty price").Value &
                                                    "', Mold_cost = '" & 清單1_dgv.Rows(i).Cells("Mold cost").Value &
                                                    "', Collected_mold_cost = '" & 清單1_dgv.Rows(i).Cells("Collected mold cost").Value &
                                                    "', Amortization_qty = '" & 清單1_dgv.Rows(i).Cells("Amortization qty").Value & "' , Profit = '" & 清單1_dgv.Rows(i).Cells("Profit").Value &
                                                    "', Commission = '" & 清單1_dgv.Rows(i).Cells("Commission").Value & "', Remark2 = '" & 清單1_dgv.Rows(i).Cells("Remark2").Value &
                                                    "', Currency = '" & ComboBox2.Text & "'
                        WHERE (利潤表流水號 LIKE '" & 清單1_dgv.Rows(i).Cells("利潤表流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    TextBox18.Text = Strings.Format(Val((i / (清單1_dgv.Rows.Count - 1)) * 100), "#,##0.0") & "%"
                Next i : ProgressBar1.Value = 0 : 最終數 = 0
            Else
                For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
                    資料數 = 清單2_dgv.Rows.Count : 進度條()
                    conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單控制表 SET  單價 = '" & 清單2_dgv.Rows(i).Cells("單價").Value & "', 總金額 = '" & 清單2_dgv.Rows(i).Cells("銷售金額").Value & "' " &
                           "WHERE (資料流水號 LIKE '" & 清單2_dgv.Rows(i).Cells("資料流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單價格利潤明細表 SET  年份 = '" & 清單2_dgv.Rows(i).Cells("年份").Value & "', Quantity = '" & 清單2_dgv.Rows(i).Cells("Quantity").Value &
                                                    "', Exchange = '" & 清單2_dgv.Rows(i).Cells("Exchange").Value & "', 審核 = '" & 清單2_dgv.Rows(i).Cells("審核").Value &
                                                    "', Price = '" & 清單2_dgv.Rows(i).Cells("Price").Value & "', Discount_price = '" & 清單2_dgv.Rows(i).Cells("Discount price").Value &
                                                    "', Commercial_invoice = '" & 清單2_dgv.Rows(i).Cells("Commercial invoice").Value &
                                                    "', Overage_invoice = '" & 清單2_dgv.Rows(i).Cells("Overage invoice").Value &
                                                    "', Selling_price = '" & 清單2_dgv.Rows(i).Cells("Selling price").Value & "', HTS_code = '" & 清單2_dgv.Rows(i).Cells("HTS code").Value &
                                                    "', Remark1 = '" & 清單2_dgv.Rows(i).Cells("Remark1").Value & "', Fty_price = '" & 清單2_dgv.Rows(i).Cells("Fty price").Value &
                                                    "', Mold_cost = '" & 清單2_dgv.Rows(i).Cells("Mold cost").Value &
                                                    "', Collected_mold_cost = '" & 清單2_dgv.Rows(i).Cells("Collected mold cost").Value &
                                                    "', Amortization_qty = '" & 清單2_dgv.Rows(i).Cells("Amortization qty").Value & "' , Profit = '" & 清單2_dgv.Rows(i).Cells("Profit").Value &
                                                    "', Commission = '" & 清單2_dgv.Rows(i).Cells("Commission").Value & "', Remark2 = '" & 清單2_dgv.Rows(i).Cells("Remark2").Value &
                                                    "', Currency = '" & ComboBox2.Text & "'
                        WHERE (利潤表流水號 LIKE '" & 清單2_dgv.Rows(i).Cells("利潤表流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                    TextBox18.Text = Strings.Format(Val((i / (清單2_dgv.Rows.Count - 1)) * 100), "#,##0.0") & "%"
                Next i : ProgressBar1.Value = 0 : 最終數 = 0

            End If
            MsgBox("修改完成")
            CheckBox1.Checked = False : CheckBox2.Checked = True
            Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_未建檔清單() : Set_grid1() : Set_自動彙總1() : Set_格式()
        End If
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If TextBox1.Text = "" Then
            MsgBox("請選擇要刪除的資料")
        Else
            Dim aa As MsgBoxResult
            aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
            If aa = MsgBoxResult.Ok Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 訂單控制表 SET  單價 = '0.00', 總金額 = '0.00' " &
                       "WHERE (訂單流水號 LIKE '" & TextBox1.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 訂單價格利潤明細表 WHERE (訂單流水號 = '" & TextBox1.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : conn.Close()
                MsgBox("刪除完成")
            End If
        End If
        Set_訂單清單1() : Set_訂單清單2() : Set_grid() : Set_grid3() : Set_未建檔清單() : Set_grid1() : Set_自動彙總1() : Set_格式()
    End Sub
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("Quantity").Value = 0 : Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("Quantity").Value = 0 : Next i
        End If
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("Quantity").Value = 清單1_dgv.Rows(i).Cells("訂單數量").Value : Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("Quantity").Value = 清單2_dgv.Rows(i).Cells("訂單數量").Value : Next i
        End If
    End Sub
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("Exchange").Value = 1 : Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("Exchange").Value = 1 : Next i
        End If
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("Currency").Value = ComboBox2.Text : Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("Currency").Value = ComboBox2.Text : Next i
        End If
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox10.Text = "LANDED BRANDED" Then
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1 : 清單1_dgv.Rows(i).Cells("年份").Value = ComboBox1.Text : Next i
        Else
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1 : 清單2_dgv.Rows(i).Cells("年份").Value = ComboBox1.Text : Next i
        End If
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If 列印用SQL = "" Then : MsgBox("請先搜尋資料後再行列印")
        Else
            xlApp = CType(CreateObject("Excel.Application"), Application)
            xlBook = xlApp.Workbooks.Add
            xlApp.DisplayAlerts = True
            xlApp.Visible = True
            xlApp.Application.WindowState = xlMaximized

            xlSheet = NewMethod(xlBook)
            xlApp.Sheets(1).Name = "FIRST COST"
            xlBook.Activate() : xlSheet.Activate()

            xlSheet.Cells(3, 1) = "BUYER" : xlSheet.Cells(3, 7) = "顏色" : xlSheet.Cells(3, 13) = "銷售金額" : xlSheet.Cells(3, 19) = "Remark1" : xlSheet.Cells(3, 25) = "Commission"
            xlSheet.Cells(3, 2) = "年份季度" : xlSheet.Cells(3, 8) = "楦型" : xlSheet.Cells(3, 14) = "備註" : xlSheet.Cells(3, 20) = "Fty price" : xlSheet.Cells(3, 26) = "Remark2"
            xlSheet.Cells(3, 3) = "訂單流水號" : xlSheet.Cells(3, 9) = "訂單數量" : xlSheet.Cells(3, 15) = "Price" : xlSheet.Cells(3, 21) = "Mold cost" : xlSheet.Cells(3, 27) = "客戶付款"
            xlSheet.Cells(3, 4) = "客戶訂單號" : xlSheet.Cells(3, 10) = "交期開始" : xlSheet.Cells(3, 16) = "Discount price" : xlSheet.Cells(3, 22) = "Collected mold cost" : xlSheet.Cells(3, 28) = "預收款項"
            xlSheet.Cells(3, 5) = "型體號碼" : xlSheet.Cells(3, 11) = "交期結束" : xlSheet.Cells(3, 17) = "Selling price" : xlSheet.Cells(3, 23) = "Amortization qty" : xlSheet.Cells(3, 29) = "成本金額"
            xlSheet.Cells(3, 6) = "型體名稱" : xlSheet.Cells(3, 12) = "單價" : xlSheet.Cells(3, 18) = "HTS code" : xlSheet.Cells(3, 24) = "Profit" : xlSheet.Cells(3, 30) = "利潤"
            xlSheet.Cells(1, 8) = "Total-PRS" : xlSheet.Cells(2, 8) = "=SUBTOTAL(9,I4:I$99999)" : xlSheet.Cells(1, 24) = "Total-Commission" : xlSheet.Cells(2, 24) = "=SUBTOTAL(9,Y4:Y$99999)"
            xlSheet.Cells(3, 31) = "實際收款" : xlSheet.Cells(3, 32) = "Currency" : xlSheet.Cells(3, 33) = "Exchange" : xlSheet.Cells(3, 34) = "年份" : xlSheet.Cells(1, 26) = "合計"
            xlSheet.Cells(1, 27) = "=SUBTOTAL(9,AA4:AA$99999)" : xlSheet.Cells(1, 28) = "=SUBTOTAL(9,AB4:AB$99999)" : xlSheet.Cells(1, 29) = "=SUBTOTAL(9,AC4:AC$99999)"
            xlSheet.Cells(1, 30) = "=SUBTOTAL(9,AD4:AD$99999)" : xlSheet.Cells(1, 31) = "=SUBTOTAL(9,AE4:AE$99999)" : xlSheet.Cells(1, 1) = "FIRST COST"
            For i As Integer = 0 To 清單2_dgv.Rows.Count - 1
                xlSheet.Cells(i + 4, 1) = 清單2_dgv.Rows(i).Cells("BUYER").Value : xlSheet.Cells(i + 4, 2) = 清單2_dgv.Rows(i).Cells("年份季度").Value
                xlSheet.Cells(i + 4, 3) = 清單2_dgv.Rows(i).Cells("訂單流水號").Value : xlSheet.Cells(i + 4, 4) = 清單2_dgv.Rows(i).Cells("客戶訂單號").Value
                xlSheet.Cells(i + 4, 5) = 清單2_dgv.Rows(i).Cells("型體號碼").Value : xlSheet.Cells(i + 4, 6) = 清單2_dgv.Rows(i).Cells("型體名稱").Value
                xlSheet.Cells(i + 4, 7) = 清單2_dgv.Rows(i).Cells("顏色").Value : xlSheet.Cells(i + 4, 8) = 清單2_dgv.Rows(i).Cells("楦型").Value
                xlSheet.Cells(i + 4, 9) = 清單2_dgv.Rows(i).Cells("訂單數量").Value : xlSheet.Cells(i + 4, 10) = 清單2_dgv.Rows(i).Cells("交期開始").Value
                xlSheet.Cells(i + 4, 11) = 清單2_dgv.Rows(i).Cells("交期結束").Value : xlSheet.Cells(i + 4, 12) = 清單2_dgv.Rows(i).Cells("單價").Value
                xlSheet.Cells(i + 4, 13) = 清單2_dgv.Rows(i).Cells("銷售金額").Value : xlSheet.Cells(i + 4, 14) = 清單2_dgv.Rows(i).Cells("備註").Value
                xlSheet.Cells(i + 4, 15) = 清單2_dgv.Rows(i).Cells("Price").Value : xlSheet.Cells(i + 4, 16) = 清單2_dgv.Rows(i).Cells("Discount price").Value
                xlSheet.Cells(i + 4, 17) = 清單2_dgv.Rows(i).Cells("Selling price").Value : xlSheet.Cells(i + 4, 18) = 清單2_dgv.Rows(i).Cells("HTS code").Value
                xlSheet.Cells(i + 4, 19) = 清單2_dgv.Rows(i).Cells("Remark1").Value : xlSheet.Cells(i + 4, 20) = 清單2_dgv.Rows(i).Cells("Fty price").Value
                xlSheet.Cells(i + 4, 21) = 清單2_dgv.Rows(i).Cells("Mold cost").Value : xlSheet.Cells(i + 4, 22) = "=U" & i + 4 & "*I" & i + 4
                xlSheet.Cells(i + 4, 23) = 清單2_dgv.Rows(i).Cells("Amortization qty").Value
                xlSheet.Cells(i + 4, 24) = "=IF((P" & i + 4 & "-U" & i + 4 & ")=0,0,(P" & i + 4 & "-T" & i + 4 & "-U" & i + 4 & ")/(P" & i + 4 & "-U" & i + 4 & "))"
                xlSheet.Cells(i + 4, 25) = "=(P" & i + 4 & "-T" & i + 4 & "-U" & i + 4 & ")*I" & i + 4 : xlSheet.Cells(i + 4, 26) = 清單2_dgv.Rows(i).Cells("Remark2").Value
                xlSheet.Cells(i + 4, 27) = "=Q" & i + 4 & "*I" & i + 4 : xlSheet.Cells(i + 4, 28) = "=(Q" & i + 4 & "-P" & i + 4 & ")*I" & i + 4
                xlSheet.Cells(i + 4, 29) = "=(T" & i + 4 & "+U" & i + 4 & ")*I" & i + 4 : xlSheet.Cells(i + 4, 30) = "=M" & i + 4 & "-AC" & i + 4
                xlSheet.Cells(i + 4, 31) = "=AA" & i + 4 & "-AC" & i + 4 : xlSheet.Cells(i + 4, 32) = 清單2_dgv.Rows(i).Cells("Currency").Value
                xlSheet.Cells(i + 4, 33) = 清單2_dgv.Rows(i).Cells("Exchange").Value : xlSheet.Cells(i + 4, 34) = 清單2_dgv.Rows(i).Cells("年份").Value
                N1 = i
            Next i
            CC(xlApp, xlSheet)

            xlSheet = NewMethod(xlBook)
            xlApp.Sheets(1).Name = "LANDED BRANDED"
            xlBook.Activate() : xlSheet.Activate()

            xlSheet.Cells(3, 1) = "BUYER" : xlSheet.Cells(3, 7) = "顏色" : xlSheet.Cells(3, 13) = "銷售金額" : xlSheet.Cells(3, 19) = "Remark1" : xlSheet.Cells(3, 25) = "Commission"
            xlSheet.Cells(3, 2) = "年份季度" : xlSheet.Cells(3, 8) = "楦型" : xlSheet.Cells(3, 14) = "備註" : xlSheet.Cells(3, 20) = "Fty price" : xlSheet.Cells(3, 26) = "Remark2"
            xlSheet.Cells(3, 3) = "訂單流水號" : xlSheet.Cells(3, 9) = "訂單數量" : xlSheet.Cells(3, 15) = "Price" : xlSheet.Cells(3, 21) = "Mold cost" : xlSheet.Cells(3, 27) = "客戶付款"
            xlSheet.Cells(3, 4) = "客戶訂單號" : xlSheet.Cells(3, 10) = "交期開始" : xlSheet.Cells(3, 16) = "Discount price" : xlSheet.Cells(3, 22) = "Collected mold cost" : xlSheet.Cells(3, 28) = "未收款項"
            xlSheet.Cells(3, 5) = "型體號碼" : xlSheet.Cells(3, 11) = "交期結束" : xlSheet.Cells(3, 17) = "Commercial invoice" : xlSheet.Cells(3, 23) = "Amortization qty" : xlSheet.Cells(3, 29) = "成本金額"
            xlSheet.Cells(3, 6) = "型體名稱" : xlSheet.Cells(3, 12) = "單價" : xlSheet.Cells(3, 18) = "Overage invoice" : xlSheet.Cells(3, 24) = "Profit" : xlSheet.Cells(3, 30) = "利潤"
            xlSheet.Cells(1, 8) = "Total-PRS" : xlSheet.Cells(2, 8) = "=SUBTOTAL(9,I4:I$99999)" : xlSheet.Cells(1, 24) = "Total-Commission" : xlSheet.Cells(2, 24) = "=SUBTOTAL(9,Y4:Y$99999)"
            xlSheet.Cells(3, 31) = "實際收款" : xlSheet.Cells(3, 32) = "Currency" : xlSheet.Cells(3, 33) = "Exchange" : xlSheet.Cells(3, 34) = "年份" : xlSheet.Cells(1, 26) = "合計"
            xlSheet.Cells(1, 27) = "=SUBTOTAL(9,AA4:AA$99999)" : xlSheet.Cells(1, 28) = "=SUBTOTAL(9,AB4:AB$99999)" : xlSheet.Cells(1, 29) = "=SUBTOTAL(9,AC4:AC$99999)"
            xlSheet.Cells(1, 30) = "=SUBTOTAL(9,AD4:AD$99999)" : xlSheet.Cells(1, 31) = "=SUBTOTAL(9,AE4:AE$99999)" : xlSheet.Cells(1, 1) = "LANDED BRANDED"
            For i As Integer = 0 To 清單1_dgv.Rows.Count - 1
                xlSheet.Cells(i + 4, 1) = 清單1_dgv.Rows(i).Cells("BUYER").Value : xlSheet.Cells(i + 4, 2) = 清單1_dgv.Rows(i).Cells("年份季度").Value
                xlSheet.Cells(i + 4, 3) = 清單1_dgv.Rows(i).Cells("訂單流水號").Value : xlSheet.Cells(i + 4, 4) = 清單1_dgv.Rows(i).Cells("客戶訂單號").Value
                xlSheet.Cells(i + 4, 5) = 清單1_dgv.Rows(i).Cells("型體號碼").Value : xlSheet.Cells(i + 4, 6) = 清單1_dgv.Rows(i).Cells("型體名稱").Value
                xlSheet.Cells(i + 4, 7) = 清單1_dgv.Rows(i).Cells("顏色").Value : xlSheet.Cells(i + 4, 8) = 清單1_dgv.Rows(i).Cells("楦型").Value
                xlSheet.Cells(i + 4, 9) = 清單1_dgv.Rows(i).Cells("訂單數量").Value : xlSheet.Cells(i + 4, 10) = 清單1_dgv.Rows(i).Cells("交期開始").Value
                xlSheet.Cells(i + 4, 11) = 清單1_dgv.Rows(i).Cells("交期結束").Value : xlSheet.Cells(i + 4, 12) = 清單1_dgv.Rows(i).Cells("單價").Value
                xlSheet.Cells(i + 4, 13) = 清單1_dgv.Rows(i).Cells("銷售金額").Value : xlSheet.Cells(i + 4, 14) = 清單1_dgv.Rows(i).Cells("備註").Value
                xlSheet.Cells(i + 4, 15) = 清單1_dgv.Rows(i).Cells("Price").Value : xlSheet.Cells(i + 4, 16) = 清單1_dgv.Rows(i).Cells("Discount price").Value
                xlSheet.Cells(i + 4, 17) = 清單1_dgv.Rows(i).Cells("Commercial invoice").Value : xlSheet.Cells(i + 4, 18) = 清單1_dgv.Rows(i).Cells("Overage invoice").Value
                xlSheet.Cells(i + 4, 19) = 清單1_dgv.Rows(i).Cells("Remark1").Value : xlSheet.Cells(i + 4, 20) = 清單1_dgv.Rows(i).Cells("Fty price").Value
                xlSheet.Cells(i + 4, 21) = 清單1_dgv.Rows(i).Cells("Mold cost").Value : xlSheet.Cells(i + 4, 22) = "=U" & i + 4 & "*I" & i + 4
                xlSheet.Cells(i + 4, 23) = 清單1_dgv.Rows(i).Cells("Amortization qty").Value
                xlSheet.Cells(i + 4, 24) = "=IF((P" & i + 4 & "-U" & i + 4 & ")=0,0,(P" & i + 4 & "-T" & i + 4 & "-U" & i + 4 & ")/(P" & i + 4 & "-U" & i + 4 & "))"
                xlSheet.Cells(i + 4, 25) = "=(P" & i + 4 & "-T" & i + 4 & "-U" & i + 4 & ")*I" & i + 4 : xlSheet.Cells(i + 4, 26) = 清單1_dgv.Rows(i).Cells("Remark2").Value
                xlSheet.Cells(i + 4, 27) = "=Q" & i + 4 & "*I" & i + 4 : xlSheet.Cells(i + 4, 28) = "=R" & i + 4 & "*I" & i + 4
                xlSheet.Cells(i + 4, 29) = "=(T" & i + 4 & "+U" & i + 4 & ")*I" & i + 4 : xlSheet.Cells(i + 4, 30) = "=M" & i + 4 & "-AC" & i + 4
                xlSheet.Cells(i + 4, 31) = "=AA" & i + 4 & "-AC" & i + 4 : xlSheet.Cells(i + 4, 32) = 清單1_dgv.Rows(i).Cells("Currency").Value
                xlSheet.Cells(i + 4, 33) = 清單1_dgv.Rows(i).Cells("Exchange").Value : xlSheet.Cells(i + 4, 34) = 清單1_dgv.Rows(i).Cells("年份").Value
                N1 = i
            Next i
            CC(xlApp, xlSheet)

            xlSheet = NewMethod(xlBook)
            xlApp.Sheets(1).Name = "彙總"
            xlBook.Activate() : xlSheet.Activate()

            xlSheet.Cells(2, 1) = "ORDER SUMMARY DATA COLLECTION"
            xlSheet.Cells(3, 2) = "平均單價" ' 
            xlSheet.Cells(3, 3) = "總雙數" : xlSheet.Cells(3, 4) = "銷售金額" : xlSheet.Cells(3, 5) = "客戶付款" : xlSheet.Cells(3, 6) = "未收款項"
            xlSheet.Cells(3, 7) = "預收款項" : xlSheet.Cells(3, 8) = "成本金額" : xlSheet.Cells(3, 9) = "利潤" : xlSheet.Cells(3, 10) = "實際收款"
            xlSheet.Cells(4, 1) = "LANDED BRANDED" : xlSheet.Cells(4, 2) = "=D4/C4"
            xlSheet.Cells(4, 3) = TextBox2.Text : xlSheet.Cells(4, 4) = TextBox3.Text : xlSheet.Cells(4, 5) = TextBox5.Text : xlSheet.Cells(4, 6) = TextBox9.Text
            xlSheet.Cells(4, 7) = TextBox21.Text : xlSheet.Cells(4, 8) = TextBox24.Text : xlSheet.Cells(4, 9) = TextBox26.Text : xlSheet.Cells(4, 10) = TextBox28.Text
            xlSheet.Cells(5, 1) = "FIRST                 COST" : xlSheet.Cells(5, 2) = "=D5/C5"
            xlSheet.Cells(5, 3) = TextBox19.Text : xlSheet.Cells(5, 4) = TextBox4.Text : xlSheet.Cells(5, 5) = TextBox11.Text : xlSheet.Cells(5, 6) = TextBox20.Text
            xlSheet.Cells(5, 7) = TextBox22.Text : xlSheet.Cells(5, 8) = TextBox23.Text : xlSheet.Cells(5, 9) = TextBox25.Text : xlSheet.Cells(5, 10) = TextBox27.Text
            xlSheet.Cells(6, 1) = "合                              計" : xlSheet.Cells(6, 2) = "=D6/C6"
            xlSheet.Cells(6, 3) = TextBox34.Text : xlSheet.Cells(6, 4) = TextBox36.Text : xlSheet.Cells(6, 5) = TextBox35.Text : xlSheet.Cells(6, 6) = TextBox33.Text
            xlSheet.Cells(6, 7) = TextBox32.Text : xlSheet.Cells(6, 8) = TextBox31.Text : xlSheet.Cells(6, 9) = TextBox30.Text : xlSheet.Cells(7, 10) = TextBox29.Text
            xlSheet.Cells(7, 6) = "PS : 應收應付如果 = 正數，及為應付；如果 = 負數，及惟應收。    預收款項 - 未收款項 = 應收應付"
            xlSheet.Cells(7, 9) = "利潤 + 應收應付 = 總實際收款" : xlSheet.Cells(7, 7) = TextBox37.Text
            AA(xlApp, xlSheet)
            xlSheet.PageSetup.PrintArea = ""
            xlApp.Cells.Select()
            xlSheet.Range("B1").Select()
            xlApp.Application.WindowState = xlMinimized
            MsgBox("列印完成")
        End If
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        xlSheet.Rows("2:7").Select : myExcel.Selection.RowHeight = 35
        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("B:J").Select : myExcel.Selection.ColumnWidth = 18

        xlSheet.Range("A2:J2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With : myExcel.Selection.Merge

        xlSheet.Range("B4:J6").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Range("G7").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Range("J7").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"

        xlSheet.Range("I7").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Range("F7").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 1
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Range("A3:J3").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Range("A2:J6").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.ActiveWindow.DisplayGridlines = False

        xlSheet.Range("C4:C6").Select() : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"

        xlSheet.Range("B3:J3").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent5 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0 : End With

        xlSheet.Range("A4:A6").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorAccent4 : .TintAndShade = 0.799981688894314 : .PatternTintAndShade = 0 : End With
    End Sub
    Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        myExcel.ActiveWindow.Zoom = 90
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With
        myExcel.Application.CutCopyMode = False
        xlSheet.Columns("D:D").Select : myExcel.Selection.NumberFormatLocal = "@"
        xlSheet.Columns("I:I").Select : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"
        xlSheet.Columns("J:K").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd"
        xlSheet.Columns("L:L").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("M:M").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("O:R").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("T:V").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("W:W").Select : myExcel.Selection.NumberFormatLocal = "#,##0;[紅色]-#,##0"
        xlSheet.Columns("X:X").Select : myExcel.Selection.NumberFormatLocal = "0.00%"
        xlSheet.Columns("Y:Y").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("AA:AE").Select : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"
        xlSheet.Columns("AG:AG").Select : myExcel.Selection.NumberFormatLocal = "#,##0.0000;[紅色]-#,##0.0000"
        xlSheet.Columns("AF:AH").Select
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With

        xlSheet.Rows("3:3").Select
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With
        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 18
        xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 16
        xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 13
        xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 5
        xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("J:K").Select : myExcel.Selection.ColumnWidth = 9.5
        xlSheet.Columns("L:L").Select : myExcel.Selection.ColumnWidth = 6
        xlSheet.Columns("M:M").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("N:N").Select : myExcel.Selection.ColumnWidth = 20
        xlSheet.Columns("O:R").Select : xlSheet.Columns("O:R").EntireColumn.AutoFit
        xlSheet.Columns("T:Y").Select : xlSheet.Columns("T:Y").EntireColumn.AutoFit
        xlSheet.Columns("AA:AH").Select : xlSheet.Columns("AA:AH").EntireColumn.AutoFit

        xlSheet.Rows("4:4").Select : myExcel.ActiveWindow.FreezePanes = True : myExcel.ActiveWindow.DisplayGridlines = False

        xlSheet.Range("A3:AH" & N1 + 5).Select() : myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 45
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlTop : .WrapText = True : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False
        End With : myExcel.Selection.AutoFilter

        xlSheet.Range("H1:I1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("H2:I2").Select()
        myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("H1:I2").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Range("A1:N3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Range("A1:N3").Select()
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .ThemeColor = xlThemeColorDark1 : .TintAndShade = -0.249977111117893 : .PatternTintAndShade = 0 : End With

        xlSheet.Range("X1:Y1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("X2:Y2").Select()
        myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("Z1:Z2").Select()
        myExcel.Selection.Merge
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("AA1:AA2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("AB1:AB2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("AC1:AC2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("AD1:AD2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("AE1:AE2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge

        xlSheet.Range("O1:Z3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .Color = 65535 : .TintAndShade = 0 : .PatternTintAndShade = 0 : End With

        xlSheet.Range("AA1:AE3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Interior : .Pattern = xlSolid : .PatternColorIndex = xlAutomatic : .Color = 5296274 : .TintAndShade = 0 : .PatternTintAndShade = 0 : End With

        xlSheet.Range("AF1:AH3").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Range("A1:G2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With : myExcel.Selection.Merge
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 16 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone
        End With

        xlSheet.Range("X3").Activate() : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("Y:Y").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Range("X2:Y2").Select() : myExcel.Selection.NumberFormatLocal = "#,##0.00;[紅色]-#,##0.00"

        xlSheet.Range("X1:Y2").Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Columns("AA:AE").Select : myExcel.Selection.ColumnWidth = 10

        xlSheet.Range("X1:Y1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("H1:I1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With
    End Sub
End Class