Dim Arr
Sub 收貨報表()
Dim Brr(), R&, K%, LotNo$
Application.ScreenUpdating = False
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
If Arr(R, 5) <> LotNo Then 新表 R: LotNo = Arr(R, 5): K = 0
K = K + 1: ReDim Preserve Brr(1 To 11, 1 To K)
Brr(1, K) = Arr(R, 9) '貨物名稱
Brr(3, K) = LotNo '批號
Brr(4, K) = Arr(R, 7) '板號
Brr(5, K) = Arr(R, 8) '箱號
Brr(8, K) = Arr(R, 10) '實物收貨
If R + 1 > UBound(Arr) Then 另存新檔 Brr: Exit For
If Arr(R + 1, 5) <> LotNo Then 另存新檔 Brr
Next R: Erase Arr: Erase Brr
Sheets("Receiving DATA").Activate
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row = 6 And Target.Column = 10 Then
Application.EnableEvents = False
Call 設下拉選單
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 6 And Target.Column = 10 Then
Application.EnableEvents = False
Call 收貨報表_單選
Application.EnableEvents = True
End If
End Sub
一般模組程式,請隨便創一個模組貼上程式即可
Dim Arr, RepNo$
Sub 設下拉選單()
Application.ScreenUpdating = False
On Error GoTo 恢復
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
RepNo = Arr(R, 13)
If D(RepNo) = 0 Then D(RepNo) = R
Next R
For Each Key In D.keys: 批號串 = 批號串 & "," & Key: Next Key
With ['Receiving Report'!J6].Validation
.Delete
.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=批號串
End With
Exit Sub
恢復: 開啟觸發事件
End Sub
Sub 收貨報表_單選()
Dim Brr(), R0%, R&, K%, D As Object
Application.ScreenUpdating = False
On Error GoTo 恢復
Arr = ['Receiving DATA'!A4].CurrentRegion
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
RepNo = Arr(R, 13)
If D(RepNo) = 0 Then D(RepNo) = R
Next R
RepNo = ['Receiving Report'!J6]
If RepNo = "" Then Exit Sub
R0 = D(RepNo): 新表 R0
For R = R0 To UBound(Arr)
K = K + 1: ReDim Preserve Brr(1 To 11, 1 To K)
Brr(1, K) = Arr(R, 9) '貨物名稱
Brr(3, K) = Arr(R, 5) '批號
Brr(4, K) = Arr(R, 7) '板號
Brr(5, K) = Arr(R, 8) '箱號
Brr(8, K) = Arr(R, 10) '實物收貨
If R + 1 > UBound(Arr) Then 另存新檔 Brr: Exit For
If Arr(R + 1, 5) <> LotNo Then 另存新檔 Brr: Exit For
Next R: Erase Arr: Erase Brr
A = 10 / 0
Exit Sub
恢復: 開啟觸發事件
End Sub
Sub 收貨報表_批次()
Dim Brr(), R&, K%
Application.ScreenUpdating = False
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
If Arr(R, 5) <> LotNo Then 新表 R: LotNo = Arr(R, 5): K = 0
K = K + 1: ReDim Preserve Brr(1 To 11, 1 To K)
Brr(1, K) = Arr(R, 9) '貨物名稱
Brr(3, K) = LotNo '批號
Brr(4, K) = Arr(R, 7) '板號
Brr(5, K) = Arr(R, 8) '箱號
Brr(8, K) = Arr(R, 10) '實物收貨
If R + 1 > UBound(Arr) Then 另存新檔 Brr: Exit For
If Arr(R + 1, 5) <> LotNo Then 另存新檔 Brr
Next R: Erase Arr: Erase Brr
Sheets("Receiving DATA").Activate
End Sub
Sub 新表(ByVal R)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Receiving Report").Copy After:=Sheets(Sheets.Count)
On Error Resume Next
Sheets("Reference No " & Arr(R, 13)).Delete
On Error GoTo 0
ActiveSheet.Name = "Reference No " & Arr(R, 13)
[C10] = Int(Arr(R, 2)) '收貨日期
[F10] = Arr(R, 2) - [C10] '收貨時間
[C11] = Arr(R, 3) '櫃號/貨車車牌
[J6] = Arr(R, 13) 'Reference No:
[J8] = Arr(R, 5) 'Lot Number
[J10] = Arr(R, 11) 'PO No.
[J11] = Arr(R, 14) 'BD 負責人
[G11] = Arr(R, 4) '封條號
刪除新表觸發事件 ActiveSheet.CodeName
End Sub
Sub 另存新檔(ByVal Brr)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
MyPath = ThisWorkbook.Path & "\"
Brr = Application.Transpose(Brr)
Rn = UBound(Brr): [G13] = Rn
If Rn >= 4 Then
Rows("21:" & 21 + Rn - 4).Insert Shift:=4
Rows(20).Copy
Rows("21:" & 21 + Rn - 4).Select
ActiveSheet.Paste
End If: [A10].Select
Application.CutCopyMode = False
[A19].Resize(Rn, 11) = Brr
[H13] = WorksheetFunction.Sum([E19].Resize(Rn))
ActiveSheet.Copy
With ActiveSheet
.Parent.SaveAs MyPath & .Name & ".xls", xlNormal
.Parent.Close 1
End With
'ActiveSheet.Delete
End Sub
Sub 刪除新表觸發事件(ByVal 新表CodeNm)
Application.DisplayAlerts = False
With ThisWorkbook.VBProject
For Each vbx In .VBComponents
If vbx.Name = 新表CodeNm Then
With vbx.CodeModule: .DeleteLines 1, .CountOfLines: End With
End If
Next
End With
End Sub
Sub 開啟觸發事件()
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row = 6 And Target.Column = 10 Then
Application.EnableEvents = False
Call 設下拉選單
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 6 And Target.Column = 10 Then
Application.EnableEvents = False
Call 收貨報表_單選
Application.EnableEvents = True
End If
End Sub
一般模組程式,請隨便創一個模組貼上程式即可
Dim Arr, RepNo$
Sub 設下拉選單()
Application.ScreenUpdating = False
On Error GoTo 恢復
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
RepNo = Arr(R, 13)
If D(RepNo) = 0 Then D(RepNo) = R
Next R
For Each Key In D.keys: 批號串 = 批號串 & "," & Key: Next Key
With ['Receiving Report'!J6].Validation
.Delete
.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=批號串
End With
Exit Sub
恢復: 開啟觸發事件
End Sub
Sub 收貨報表_單選()
Dim Brr(), R0%, R&, K%, D As Object
Application.ScreenUpdating = False
On Error GoTo 恢復
RepNo = ['Receiving Report'!J6]
If RepNo = "" Then Exit Sub
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
If D(Arr(R, 13) & "") = 0 Then D(Arr(R, 13) & "") = R
Next R
R0 = D(RepNo): 新表 R0
For R = R0 To UBound(Arr)
K = K + 1: ReDim Preserve Brr(1 To 11, 1 To K)
Brr(1, K) = Arr(R, 9) '貨物名稱
Brr(3, K) = Arr(R, 5) '批號
Brr(4, K) = Arr(R, 7) '板號
Brr(5, K) = Arr(R, 8) '箱號
Brr(8, K) = Arr(R, 10) '實物收貨
If R + 1 > UBound(Arr) Then 另存新檔 Brr: Exit For
If Arr(R + 1, 13) <> RepNo Then 另存新檔 Brr: Exit For
Next R: Erase Arr: Erase Brr
Exit Sub
恢復: 開啟觸發事件
End Sub
Sub 收貨報表_批次()
Dim Brr(), R&, K%
Application.ScreenUpdating = False
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
If Arr(R, 13) <> RepNo Then 新表 R: RepNo = Arr(R, 13): K = 0
K = K + 1: ReDim Preserve Brr(1 To 11, 1 To K)
Brr(1, K) = Arr(R, 9) '貨物名稱
Brr(3, K) = LotNo '批號
Brr(4, K) = Arr(R, 7) '板號
Brr(5, K) = Arr(R, 8) '箱號
Brr(8, K) = Arr(R, 10) '實物收貨
If R + 1 > UBound(Arr) Then 另存新檔 Brr: Exit For
If Arr(R + 1, 13) <> RepNo Then 另存新檔 Brr
Next R: Erase Arr: Erase Brr
Sheets("Receiving DATA").Activate
End Sub
Sub 新表(ByVal R)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Receiving Report").Copy After:=Sheets(Sheets.Count)
On Error Resume Next
Sheets("Reference No " & Arr(R, 13)).Delete
On Error GoTo 0
ActiveSheet.Name = "Reference No " & Arr(R, 13)
[C10] = Int(Arr(R, 2)) '收貨日期
[F10] = Arr(R, 2) - [C10] '收貨時間
[C11] = Arr(R, 3) '櫃號/貨車車牌
[J6] = Arr(R, 13) 'Reference No:
[J8] = Arr(R, 5) 'Lot Number
[J10] = Arr(R, 11) 'PO No.
[J11] = Arr(R, 14) 'BD 負責人
[G11] = Arr(R, 4) '封條號
刪除新表觸發事件 ActiveSheet.CodeName
[J6].Validation.Delete
End Sub
Sub 另存新檔(ByVal Brr)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
MyPath = ThisWorkbook.Path & "\"
Brr = Application.Transpose(Brr) '當Brr資料只有一筆,轉置後變1維陣列
Rn = UBound(Brr): [G13] = Rn
On Error Resume Next
XX% = UBound(Brr, 2) '當Brr=1維陣列,會跳錯誤
If Err <> 0 Then Rn = 1 '錯誤時,設Rn=1
On Error GoTo 0
If Rn >= 4 Then
Rows("21:" & 21 + Rn - 4).Insert Shift:=4
Rows(20).Copy
Rows("21:" & 21 + Rn - 4).Select
ActiveSheet.Paste
End If: [A10].Select
Application.CutCopyMode = False
[A19].Resize(Rn, 11) = Brr
[H13] = WorksheetFunction.Sum([E19].Resize(Rn))
ActiveSheet.Copy
With ActiveSheet
.Parent.SaveAs MyPath & .Name & ".xls", xlNormal
.Parent.Close 1
End With
'ActiveSheet.Delete
End Sub
Sub 刪除新表觸發事件(ByVal 新表CodeNm)
Application.DisplayAlerts = False
With ThisWorkbook.VBProject
For Each vbx In .VBComponents
If vbx.Name = 新表CodeNm Then
With vbx.CodeModule: .DeleteLines 1, .CountOfLines: End With
End If
Next
End With
End Sub
Sub 開啟觸發事件()
Application.EnableEvents = True
End Sub
Sub 設下拉選單()
Application.ScreenUpdating = False
On Error GoTo 恢復
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
RepNo = Arr(R, 13)
If D(RepNo) = 0 Then D(RepNo) = R
Next R
For Each Key In D.keys: 批號串 = 批號串 & "," & Key: Next Key
With ['Receiving Report'!J6].Validation
.Delete
.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=批號串
End With
Exit Sub
恢復: 開啟觸發事件
End Sub
請問這個批號串是出自那裡,因為我試把那數字組轉成別的組合,發現英文+數字的組合可行,像apr 0001, 但如若只0001或是 aaa-01/12-0001 這種便不行,這個我嘗試入格式換掉[批號串]﹐也是不行,是否這種語法本來就不可以變改的呢?作者: 准提部林 時間: 2021-1-7 20:27
Sub 設下拉選單()
Application.ScreenUpdating = False
On Error GoTo 恢復
Set D = CreateObject("Scripting.Dictionary")
Arr = ['Receiving DATA'!A4].CurrentRegion
For R = 2 To UBound(Arr)
RepNo = Arr(R, 13)
If D(RepNo) = 0 Then D(RepNo) = R
Next R]作者: lovenice831 時間: 2021-1-8 12:34