|
Sub hebing()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim j As Integer
Dim k As Integer
' 获取采购单工作表和出库单工作表
Set sourceSheet = Sheets("采购单")
Set targetSheet = Sheets("出库单")
' 清空出库单内容
targetSheet.Range("B3:D180").ClearContents
' 将“不易坏的月初采购”数据添加到出库单中
targetSheet.Range("B3").Value = "不易坏的月初采购"
targetSheet.Range("C3").Value = "日期"
targetSheet.Range("D3").Value = "数量"
lastRow = sourceSheet.Cells(Rows.Count, "B").End(xlUp).Row
j = 4
For i = 19 To lastRow
If sourceSheet.Cells(i, "B").Value = "不易坏的月初采购" Then
targetSheet.Cells(j, "B").Value = sourceSheet.Cells(i, "B").Value
targetSheet.Cells(j, "C").Value = DateSerial(Year(sourceSheet.Cells(i, "C").Value), Month(sourceSheet.Cells(i, "C").Value), 1)
targetSheet.Cells(j, "D").Value = sourceSheet.Cells(i, "D").Value
j = j + 1
End If
Next i
' 将“蔬菜水果和肉按周采购”数据添加到出库单中
targetSheet.Range("B52").Value = "蔬菜水果和肉按周采购"
targetSheet.Range("C52").Value = "日期"
targetSheet.Range("D52").Value = "数量"
j = 53
k = 1
For i = 19 To lastRow
If sourceSheet.Cells(i, "B").Value = "蔬菜水果和肉按周采购" Then
Dim weekStart As Date
weekStart = DateSerial(Year(sourceSheet.Cells(i, "C").Value), Month(sourceSheet.Cells(i, "C").Value), Day(sourceSheet.Cells(i, "C").Value) - Weekday(sourceSheet.Cells(i, "C").Value, vbMonday) + 1)
targetSheet.Cells(j, "B").Value = sourceSheet.Cells(i, "B").Value
For k = 1 To 7
targetSheet.Cells(j, "C").Value = weekStart + k - 1
targetSheet.Cells(j, "D").Value = ""
j = j + 1
Next k
' 在当前行的前一列中填写数量
targetSheet.Cells(j - 8, "D").Value = sourceSheet.Cells(i, "D").Value
End If
Next i
' 删除多余的空行
lastRow = targetSheet.Cells(Rows.Count, "B").End(xlUp).Row
targetSheet.Rows(lastRow + 1 & ":" & 180).Delete Shift:=xlUp
' 自动补足空行
j = targetSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
For i = j To 180
targetSheet.Cells(i, "B").Value = ""
targetSheet.Cells(i, "C").Value = ""
targetSheet.Cells(i, "D").Value = ""
Next i
MsgBox "合并完成!"
End Sub |
|