楼主 kevinchengcw |
根据本社区一个帖子提出的问题写出的一个出库单自动生成样本,相信很多有同样需要的人,拿出来与大家一起分享,见附件
共分成三部来完成: 一:提取客户信息存入“客户资料”工作表 二:生成出库汇总(出库货品清单,出库单号及合格证号) 三:生成要打印的出库单表格
全部代码如下:- Private Sub CommandButton1_Click()
- Dim Arr, Arr1, Arr2, Dic
- Dim M, N, R, No, I As Integer
- Set Dic = CreateObject("scripting.dictionary")
- '步骤一:
- Arr = Split("b2,e2,g2,B3,e3,G3", ",") '生成要提取的客户资料于工作单中存放的单元格地址数组
- M = Worksheets("客户资料").[a65536].End(3).Row + 1 '取得客户资料中的要写入的行地址
- For N = 0 To UBound(Arr) '循环提取客户资料
- Worksheets("客户资料").Cells(M, N + 1) = Worksheets("工作单").Range(Arr(N))
- Next N
- '步骤二:
- With Worksheets("工作单")
- For N = 5 To .[g65536].End(3).Row '循环取值,并利用字典去除重复项
- If Not Dic.exists(.Cells(N, 7).Value) Then Dic.Add .Cells(N, 7).Value, .Cells(N, 6).Value
- Next N
- End With
- I = 1
- M = Worksheets("出库汇总").[a65536].End(3).Row + 1 '确定出库汇总中的续写行数起始值
- For N = 5 To Worksheets("工作单").[a65536].End(3).Row '循环写入工作单只对应数据到出库汇总表中
- With Worksheets("工作单")
- Worksheets("出库汇总").Cells(M, 1) = .[b2]
- Worksheets("出库汇总").Cells(M, 2) = .[e2]
- Worksheets("出库汇总").Cells(M, 4) = .Cells(N, 6)
- Worksheets("出库汇总").Cells(M, 5) = .Cells(N, 7)
- If Day(Worksheets("出库汇总").Cells(M - 1, 2)) = Day(Worksheets("出库汇总").Cells(M, 2)) And M > 2 Then
- If Worksheets("出库汇总").Cells(M, 4) = Worksheets("出库汇总").Cells(M - 1, 4) And Worksheets("出库汇总").Cells(M, 5) = Worksheets("出库汇总").Cells(M - 1, 5) Then
- Worksheets("出库汇总").Cells(M, 3) = Worksheets("出库汇总").Cells(M - 1, 3)
- Else
- Worksheets("出库汇总").Cells(M, 3) = .[g1] & "-" & Val(Replace(Right(Worksheets("出库汇总").Cells(M - 1, 3), 2), "-", "")) + 1
- End If
- Else
- Worksheets("出库汇总").Cells(M, 3) = .[g1] & "-1"
- End If
- Worksheets("出库汇总").Cells(M, 6) = ""
- Worksheets("出库汇总").Cells(M, 7) = .Cells(N, 1)
- Worksheets("出库汇总").Cells(M, 8) = .Cells(N, 2)
- Worksheets("出库汇总").Cells(M, 9) = .Cells(N, 3)
- Worksheets("出库汇总").Cells(M, 10) = .Cells(N, 4)
- Worksheets("出库汇总").Cells(M, 11) = .Cells(N, 5)
- If Month(Worksheets("出库汇总").Cells(M, 2)) = Month(Worksheets("出库汇总").Cells(M - 1, 2)) Then
- If Worksheets("出库汇总").Cells(M, 9) > 1 Then
- Worksheets("出库汇总").Cells(M, 12) = Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & Format(Val(Right(Worksheets("出库汇总").Cells(M - 1, 12), 2)) + 1, "00") & "-" & Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & Format(Val(Right(Worksheets("出库汇总").Cells(M - 1, 12), 2)) + Worksheets("出库汇总").Cells(M, 9), "00")
- Else
- Worksheets("出库汇总").Cells(M, 12) = Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & Format(Val(Right(Worksheets("出库汇总").Cells(M - 1, 12), 2)) + 1, "00")
- End If
- Else
- If Worksheets("出库汇总").Cells(M, 9) > 1 Then
- Worksheets("出库汇总").Cells(M, 12) = Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & "01-" & Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & Format(Worksheets("出库汇总").Cells(M, 9), "00")
- Else
- Worksheets("出库汇总").Cells(M, 12) = Format(Worksheets("出库汇总").Cells(M, 2), "YYYYMM") & "01"
- End If
- End If
- M = M + 1
- End With
- Next N
- '步骤三:
- Arr1 = Dic.keys '生成写入项数组
- Arr2 = Split("器具名称,规格,数量,单价,价格,合格证号,其他说明", ",") '生成标题项数组
- R = 1
- With Worksheets("出库单") '删除旧有单据
- .Columns("A:H").Insert shift:=xlToRight
- .Columns("I:P").Delete
- .Columns("A:H").EntireColumn.AutoFit
- '.Columns("A:H").Cells.MergeCells = False
- End With
- For No = 0 To UBound(Arr1) '循环生成出库单
- With Worksheets("出库单")
- .Cells(R, 1) = "出库单"
- .Cells(R, 1).Offset(1, 0) = "单位名称"
- .Cells(R, 1).Offset(1, 1) = Worksheets("工作单").Range(Arr(0))
- .Cells(R, 1).Offset(1, 3) = "销售日期"
- .Cells(R, 1).Offset(1, 4) = Worksheets("工作单").Range(Arr(1))
- .Cells(R, 1).Offset(1, 4).NumberFormatLocal = "yyyy-m-d"
- .Cells(R, 1).Offset(1, 5) = "出库单号"
- .Cells(R, 1).Offset(1, 6) = Worksheets("工作单").Range("g1").Value & "-" & No + 1
- .Cells(R, 1).Offset(2, 0) = "所属类别"
- .Cells(R, 1).Offset(2, 1) = Dic.Item(Arr1(No))
- .Cells(R, 1).Offset(2, 3) = "所属项目"
- .Cells(R, 1).Offset(2, 4) = Arr1(No)
- .Cells(R, 1).Offset(2, 5) = "出库日期"
- .Cells(R, 1).Offset(2, 6) = ""
- .Range(.Cells(R, 1).Offset(2, 1), .Cells(R, 1).Offset(2, 2)).MergeCells = True
- .Range(.Cells(R, 1).Offset(1, 1), .Cells(R, 1).Offset(1, 2)).MergeCells = True
- For M = 0 To UBound(Arr2)
- .Cells(R, 1).Offset(3, M) = Arr2(M)
- Next M
- I = 0
- For N = 3 To Worksheets("出库汇总").[a65536].End(3).Row
- If Worksheets("出库汇总").Cells(N, 3) = .Cells(R, 1).Offset(1, 6) Then
- .Cells(R, 1).Offset(4 + I, 0) = Worksheets("出库汇总").Cells(N, 7)
- .Cells(R, 1).Offset(4 + I, 1) = Worksheets("出库汇总").Cells(N, 8)
- .Cells(R, 1).Offset(4 + I, 2) = Worksheets("出库汇总").Cells(N, 9)
- .Cells(R, 1).Offset(4 + I, 3) = Worksheets("出库汇总").Cells(N, 10)
- .Cells(R, 1).Offset(4 + I, 4) = Worksheets("出库汇总").Cells(N, 11)
- .Cells(R, 1).Offset(4 + I, 5) = Worksheets("出库汇总").Cells(N, 12)
- I = I + 1
- End If
- Next N
- .Range(.Cells(R, 1), .Cells(R, 7)).MergeCells = True '完成表格及格式设定
- M = .[a65536].End(3).Row
- .Range(.Cells(R, 1), .Cells(M, 7)).Borders.LineStyle =1
- With .Range(.Cells(R, 1), .Cells(M, 7))
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .WrapText = False
- .Orientation = 0
- .AddIndent = False
- .IndentLevel = 0
- .ShrinkToFit = False
- .ReadingOrder = xlContext
- End With
- For N = 1 To 7
- .Columns(N).EntireColumn.AutoFit
- Next N
- R = M + 3
- End With
- Next No
- End Sub
销售出库单.rar |