|
Sub 生成领料单()
Application.ScreenUpdating = False
Dim d As Object
Dim ar As Variant
Dim br()
Dim i As Long
Set d = CreateObject("scripting.dictionary")
f = Dir(ThisWorkbook.Path & "\源数据.xls*")
If f = "" Then MsgBox "找不到源数据文件!": End
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & f, 0)
ar = wb.Worksheets(1).[a1].CurrentRegion
wb.Close False
For i = 3 To UBound(ar)
If Trim(ar(i, 1)) <> "" And Trim(ar(i, 7)) <> "" Then
zf = Trim(ar(i, 1)) & "|" & Trim(ar(i, 7))
d(zf) = ""
End If
Next i
Set Rng = Sheets("模板").Rows("1:20")
Set sh = Sheets("领料单")
With sh
.UsedRange.Clear
For Each k In d.keys
n = 0
ReDim br(1 To UBound(ar), 1 To 7)
For i = 3 To UBound(ar)
zf = Trim(ar(i, 1)) & "|" & Trim(ar(i, 7))
If zf = k Then
n = n + 1
rq = ar(i, 1)
br(n, 1) = n
For j = 2 To 6
br(n, j) = ar(i, j)
Next j
br(n, 7) = br(n, 6)
End If
Next i
If n <= 10 Then
r = .Cells(Rows.Count, 2).End(xlUp).Row + 2
If r = 3 Then
r = 1
Else
r = r
End If
Rng.Copy .Cells(r, 1)
.Cells(r + 2, 10) = rq
.Cells(r + 2, 5) = Split(k, "|")(1)
If r = 1 Then
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(1, "00")
ElseIf r > 1 Then
If Val(Format(.Cells(r - 18, 10), "yyyymmdd")) = Val(Format(rq, "yyyymmdd")) Then
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(Val(Right(.Cells(r - 19, 6), 2)) + 1, "00")
Else
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(1, "00")
End If
End If
.Cells(r + 4, 1).Resize(n, UBound(br, 2)) = br
ElseIf n > 10 Then
For i = 1 To n Step 10
r = .Cells(Rows.Count, 2).End(xlUp).Row + 2
If r = 3 Then
r = 1
Else
r = r
End If
Rng.Copy .Cells(r, 1)
.Cells(r + 2, 10) = rq
.Cells(r + 2, 5) = Split(k, "|")(1)
If r = 1 Then
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(1, "00")
ElseIf r > 1 Then
rq_1 = Val(Format(.Cells(r - 18, 10), "yyyymmdd"))
rq_2 = Val(Format(rq, "yyyymmdd"))
a = Val(Right(.Cells(r - 19, 6), 2))
w = Format(Val(Right(.Cells(r - 18, 10), 2)) + 1, "00")
If Val(Format(.Cells(r - 18, 10), "yyyymmdd")) = Val(Format(rq, "yyyymmdd")) Then
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(Val(Right(.Cells(r - 19, 6), 2)) + 1, "00")
Else
.Cells(r + 1, 6) = "YFLL" & Format(rq, "yyyymmdd") & Format(1, "00")
End If
End If
xh = r + 3
For s = i To i + 9
xh = xh + 1
For j = 1 To 7
.Cells(xh, j) = br(s, j)
Next j
Next s
Next i
End If
Next k
End With
Application.ScreenUpdating = True
MsgBox "ok!", 64, "提醒!"
End Sub
|
|