Sub 選取檔案()
Dim 開啟檔案路徑 As Variant
Dim i As Integer
Dim targetWorkbook As Workbook
Dim openedWorkbook As Workbook
k = 1
' 使用.GetOpenFilename方法的多選選項
開啟檔案路徑 = Application.GetOpenFilename("Excel Files (*.xls; *.xlsx), *.xls; *.xlsx", , "選擇要匯入的檔案", , True)
' 如果沒有選取檔案,則結束
If IsArray(開啟檔案路徑) = False Then Exit Sub
' 設定目標工作簿
Set targetWorkbook = ActiveWorkbook
' 逐一開啟每個選取的檔案
For i = LBound(開啟檔案路徑) To UBound(開啟檔案路徑)
Set openedWorkbook = Workbooks.Open(開啟檔案路徑(i))
' 將第一個工作表複製到目標工作簿的"BOM生成區"之後
openedWorkbook.Sheets(1).Copy After:=targetWorkbook.Sheets("BOM生成區")
openedWorkbook.Close False
Next i
' 選擇剛複製的工作表
targetWorkbook.Sheets("BOM生成區").Select
' 清理工作簿
失效清除
End Sub
For coun_num1 = 2 To ws2_data_end_num
If Cells(coun_num1, 屬性欄位) = "託外加工件" Or Cells(coun_num1, 屬性欄位) = "虛設品號" Or Cells(coun_num1, 屬性欄位) = "自製件" Then
Cells(coun_num1, 標準批量欄位) = 1
code
Else
Cells(coun_num1, 標準批量欄位) = 0
End If
Next
End Sub
Sub 失效清除()
Dim aRng As Range
f = Cells(Rows.Count, 1).End(xlUp).Row
g = Cells(1, Columns.Count).End(xlToLeft).Column
Set aRng = Cells.Find(what:="失效日期", LookAt:=xlWhole)
Set bRng = Cells.Find(what:="階 次", LookAt:=xlWhole)
If aRng Is Nothing Then Exit Sub
If bRng Is Nothing Then Exit Sub
k1 = aRng.Column
k2 = aRng.Row
j1 = bRng.Column
j2 = bRng.Row
For s1 = 1 To f Step 1
code
If Cells(s1, k1) <> "" And Cells(s1, k1) <> "失效日期" Then
u1 = Right(Cells(s1, j1), 1)
Do
Cells(s1, 31) = "V"
s1 = s1 + 1
If s1 = f + 1 Then Exit Sub
Loop Until Right(Cells(s1, j1), 1) = u1
If Cells(s1, k1) <> "" Then
s1 = s1 - 1
End If
End If
Next
For s2 = f To 1 Step -1
code
If Cells(s2, 31) = "V" Then
Rows(s2).Delete
End If
Sub BOM生成()
Dim t1, t2 As String
Dim k As Integer
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
k = 1
On Error GoTo special
ChDrive "C:"
ChDir "C:\My Documents"
選取檔案
If ActiveWorkbook.Worksheets.Count = 1 Then
MsgBox ("未匯入資料,請重新匯入")
GoTo fini
End If
' 驗證資料格式
If Worksheets(2).Cells(2, 2) <> "0" Then
MsgBox ("匯入資料格式異常")
Worksheets(2).Delete
GoTo fini
End If
工作頁面清潔
Worksheets(2).Activate
資料表頭整理
半成品件判斷
ws2_data_end_num = Cells(65536, "B").End(xlUp).Row
ws2_mark_end_num = Cells(1, 256).End(xlToLeft).Column
ws2_data_num = ws2_data_end_num - 2
ws1_num = 1
' 找到各欄位位置
序號欄位 = 目標欄位檢索(1, "序號", ws2_mark_end_num)
元件品號欄位 = 目標欄位檢索(1, "元件品號", ws2_mark_end_num)
屬性欄位 = 目標欄位檢索(1, "屬性", ws2_mark_end_num)
品名欄位 = 目標欄位檢索(1, "品名", ws2_mark_end_num)
規格欄位 = 目標欄位檢索(1, "規格", ws2_mark_end_num)
插件位置欄位 = 目標欄位檢索(1, "插件位置1", ws2_mark_end_num)
組成用量欄位 = 目標欄位檢索(1, "組成用量", ws2_mark_end_num)
底數欄位 = 目標欄位檢索(1, "底數", ws2_mark_end_num)
單位欄位 = 目標欄位檢索(1, "單位", ws2_mark_end_num)
標準批量欄位 = 目標欄位檢索(1, "標準批量", ws2_mark_end_num)
階次欄位 = 目標欄位檢索(1, "階次", ws2_mark_end_num)
損耗率欄位 = 目標欄位檢索(1, "損耗率%", ws2_mark_end_num)
類型欄位 = 目標欄位檢索(1, "類型", ws2_mark_end_num)
For ws2_count_num = 2 To ws2_data_num Step 1
' 檢查階次和標準批量的條件
If Worksheets(2).Cells(ws2_count_num, 階次欄位) <> "" And Worksheets(2).Cells(ws2_count_num, 標準批量欄位) = 1 Then
Worksheets(1).Cells(ws1_num, "B") = Worksheets(2).Cells(ws2_count_num, 元件品號欄位)
Worksheets(1).Cells(ws1_num, "C") = Worksheets(2).Cells(ws2_count_num, 品名欄位)
Worksheets(1).Cells(ws1_num, "B").Interior.ColorIndex = 6
Worksheets(1).Cells(ws1_num, "C").HorizontalAlignment = xlLeft
Worksheets(1).Cells(ws1_num, "C").Interior.ColorIndex = 6
mark_ini_num = ws1_num
ws1_num = ws1_num + 1
' 寫入表頭
Worksheets(1).Cells(ws1_num, "A") = "序號"
Worksheets(1).Cells(ws1_num, "B") = "元件品號"
Worksheets(1).Cells(ws1_num, "C") = "品名"
Worksheets(1).Cells(ws1_num, "D") = "規格"
Worksheets(1).Cells(ws1_num, "E") = "組成用量"
Worksheets(1).Cells(ws1_num, "F") = "底數"
Worksheets(1).Cells(ws1_num, "G") = "單位"
Worksheets(1).Cells(ws1_num, "H") = "損耗率%"
Worksheets(1).Cells(ws1_num, "I") = "插件位置"
Worksheets(1).Cells(ws1_num, "J") = "屬性"
Worksheets(1).Cells(ws1_num, "K") = "貨號"
ws1_num = ws1_num + 1
ws2_loop_num = 1 + ws2_count_num
' 處理資料行
Do While Worksheets(2).Cells(ws2_count_num, 階次欄位) <> ""
t1 = Val(Right(Worksheets(2).Cells(ws2_count_num, "B"), 1))
t2 = Val(Right(Worksheets(2).Cells(ws2_loop_num, "B"), 1))
If t2 - t1 = 0 Then
Exit Do
ElseIf t2 - t1 = 1 Then
Worksheets(1).Cells(ws1_num, "A") = Worksheets(2).Cells(ws2_loop_num, 序號欄位)
Worksheets(1).Cells(ws1_num, "A").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "B") = Worksheets(2).Cells(ws2_loop_num, 元件品號欄位)
Worksheets(1).Cells(ws1_num, "B").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "C") = Worksheets(2).Cells(ws2_loop_num, 品名欄位)
Worksheets(1).Cells(ws1_num, "C").HorizontalAlignment = xlLeft
Worksheets(1).Cells(ws1_num, "D") = Worksheets(2).Cells(ws2_loop_num, 規格欄位)
Worksheets(1).Cells(ws1_num, "D").HorizontalAlignment = xlLeft
Worksheets(1).Cells(ws1_num, "E") = Worksheets(2).Cells(ws2_loop_num, 組成用量欄位)
Worksheets(1).Cells(ws1_num, "E").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "F") = Worksheets(2).Cells(ws2_loop_num, 底數欄位)
Worksheets(1).Cells(ws1_num, "F").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "G") = Worksheets(2).Cells(ws2_loop_num, 單位欄位)
Worksheets(1).Cells(ws1_num, "G").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "H") = Worksheets(2).Cells(ws2_loop_num, 損耗率欄位)
Worksheets(1).Cells(ws1_num, "H").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "I") = Worksheets(2).Cells(ws2_loop_num, 插件位置欄位)
Worksheets(1).Cells(ws1_num, "I").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "J") = Worksheets(2).Cells(ws2_loop_num, 屬性欄位)
Worksheets(1).Cells(ws1_num, "J").HorizontalAlignment = xlCenter
Worksheets(1).Cells(ws1_num, "K") = Worksheets(2).Cells(ws2_loop_num, 類型欄位)
Worksheets(1).Cells(ws1_num, "K").HorizontalAlignment = xlCenter
If Worksheets(2).Cells(ws2_loop_num, 標準批量欄位) = 1 Then
Worksheets(1).Activate
Worksheets(1).Range(Cells(ws1_num, "A"), Cells(ws1_num, "K")).Font.ColorIndex = 7
End If
ws1_num = ws1_num + 1
End If
ws2_loop_num = ws2_loop_num + 1
If ws2_loop_num > ws2_data_end_num Then Exit Do
Loop
' 標記邊框
mark_end_num = ws1_num - 1
Worksheets(1).Activate
Worksheets(1).Range(Cells(mark_ini_num + 1, 1), Cells(mark_end_num, "K")).Borders.LineStyle = xlContinuous
Worksheets(1).Range(Cells(mark_ini_num + 1, 1), Cells(mark_end_num, "K")).BorderAround Weight:=xlMedium
Worksheets(1).Range(Cells(mark_ini_num, 2), Cells(mark_ini_num, 3)).BorderAround Weight:=xlMedium
ws1_num = ws1_num + 1
End If
Next
Columns("A:K").AutoFit
Columns("A:P").Select
ActiveWindow.Zoom = True
Cells(1, 1).Select
GoTo fini
special:
MsgBox "發生錯誤,請與相關人員聯繫。"
fini:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
End Sub
Sub 資料表頭整理()
Dim coun_num1 As Integer, coun_num2 As Integer
Dim 主件品號欄位 As Integer, 元件品號欄位 As Integer
Dim 資料表頭尾端 As Integer, 資料表串尾端 As Integer
資料表頭尾端 = Cells(1, 256).End(xlToLeft).Column
資料表串尾端 = Cells(65536, "E").End(xlUp).Row
For coun_num2 = 1 To 資料表串尾端
Cells(1, coun_num2) = Replace(Cells(1, coun_num2), " ", "")
Next
主件品號欄位 = 目標欄位檢索(1, "主件品號", 資料表頭尾端)
元件品號欄位 = 目標欄位檢索(1, "元件品號", 資料表頭尾端)
For coun_num1 = 1 To 資料表串尾端
If Cells(coun_num1, 元件品號欄位) = "" Then
Cells(coun_num1, 元件品號欄位) = Cells(coun_num1, 主件品號欄位)
End If
Next
For coun_num2 = 1 To 資料表串尾端
Cells(1, coun_num2) = Replace(Cells(1, coun_num2), " ", "")
Next
End Sub
Sub 工作頁面清潔()
Worksheets(1).Activate
Columns("A:K").Clear
Columns("A:K").HorizontalAlignment = xlCenter
Columns("A:K").ClearContents
End Sub
Sub 失效清除()
Dim aRng As Range
Dim bRng As Range
Dim f As Long, g As Long
Dim k1 As Integer, k2 As Integer
Dim j1 As Integer, j2 As Integer
Dim s1 As Long
f = Cells(Rows.Count, 1).End(xlUp).Row
g = Cells(1, Columns.Count).End(xlToLeft).Column
Set aRng = Cells.Find(what:="失效日期", LookAt:=xlWhole)
Set bRng = Cells.Find(what:="階 次", LookAt:=xlWhole)
If aRng Is Nothing Then Exit Sub
If bRng Is Nothing Then Exit Sub
k1 = aRng.Column
k2 = aRng.Row
j1 = bRng.Column
j2 = bRng.Row
For s1 = 1 To f
If Cells(s1, k1) <> "" And Cells(s1, k1) <> "失效日期" Then
u1 = Right(Cells(s1, j1), 1)
Do
Cells(s1, 31) = "V"
s1 = s1 + 1
If s1 = f + 1 Then Exit Sub
Loop Until Right(Cells(s1, j1), 1) = u1
If Cells(s1, k1) <> "" Then
s1 = s1 - 1
End If
End If
Next
For s2 = f To 1 Step -1
If Cells(s2, 31) = "V" Then
Rows(s2).Delete
End If
Next
End Sub
Function 目標欄位檢索(ByVal 檢索列 As Integer, ByVal 檢索目標 As String, ByVal 資料尾端 As Integer) As Integer
Dim coun_num As Integer
For coun_num = 1 To 資料尾端
If Cells(檢索列, coun_num) = 檢索目標 Then
Exit For
End If
Next
目標欄位檢索 = coun_num
End Function
Sub 選取檔案()
Dim 開啟檔案路徑 As Variant
Dim i As Integer
Dim targetWorkbook As Workbook
Dim openedWorkbook As Workbook
k = 1
開啟檔案路徑 = Application.GetOpenFilename("Excel Files (*.xls; *.xlsx), *.xls; *.xlsx", , "選擇要匯入的檔案", , True)
If IsArray(開啟檔案路徑) = False Then Exit Sub
Set targetWorkbook = ActiveWorkbook
For i = LBound(開啟檔案路徑) To UBound(開啟檔案路徑)
Set openedWorkbook = Workbooks.Open(開啟檔案路徑(i))
' 將第一個工作表複製到目標工作簿的"BOM生成區"之後
openedWorkbook.Sheets(1).Copy After:=targetWorkbook.Sheets("BOM生成區")
openedWorkbook.Close False
Next i
targetWorkbook.Sheets("BOM生成區").Select
失效清除
End Sub
Sub 半成品件判斷()
Worksheets(2).Activate
ws2_data_end_num = Cells(65536, "B").End(xlUp).Row
ws2_mark_end_num = Cells(1, 256).End(xlToLeft).Column
標準批量欄位 = 目標欄位檢索(1, "標準批量", ws2_mark_end_num)
屬性欄位 = 目標欄位檢索(1, "屬性", ws2_mark_end_num)
For coun_num1 = 2 To ws2_data_end_num
If Cells(coun_num1, 屬性欄位) = "託外加工件" Or Cells(coun_num1, 屬性欄位) = "虛設品號" Or Cells(coun_num1, 屬性欄位) = "自製件" Then
Cells(coun_num1, 標準批量欄位) = 1
Else
Cells(coun_num1, 標準批量欄位) = 0
End If
Next
End Sub