|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 收款单据生成()
- Dim d, i%, j%, lastrow%, startrow%, startdate, enddate
- Dim arr1, arr2
- Set d = CreateObject("scripting.dictionary")
- lastrow = Sheets(1).Range("A65536").End(3).Row
- arr1 = Sheets(1).Range("A2:K" & lastrow)
- arr2 = Sheets(2).Range("A1:H10")
- startdate = CDate(InputBox("请输入开始日期:例如2018/1/1", "开始日期"))
- enddate = CDate(InputBox("请输入结束日期:例如2018/12/31", "结束日期"))
- For i = 1 To UBound(arr1)
- If arr1(i, 2) >= startdate And arr1(i, 2) <= enddate Then
- d(CStr(arr1(i, 3))) = ""
- End If
- Next
- With Sheets(4) '这里我新建了一个工作表,如果你想直接在第三个工作表生成结果,把4改成3即可
- .UsedRange.Clear
- For i = 1 To d.Count
- If .Range("A1") = "" And .Range("A2") = "" Then
- startrow = 2
- Else
- startrow = .Range("A65536").End(3).Row + 2
- End If
- Sheets(2).Range("A1:H10").Copy
- .Cells(startrow, 1).PasteSpecial Paste:=xlPasteColumnWidths
- .Cells(startrow, 1).PasteSpecial Paste:=xlPasteAll
- Next
- .Rows("1:10000").RowHeight = 25
- d.RemoveAll
- For j = 1 To UBound(arr1)
- If arr1(j, 2) >= startdate And arr1(j, 2) <= enddate Then
- If Not d.exists(CStr(arr1(j, 3))) Then
- d(CStr(arr1(j, 3))) = (d.Count) * 11 + 5
- .Cells((d.Count - 1) * 11 + 3, 1) = .Cells((d.Count - 1) * 11 + 3, 1) & Sheets(1).Cells(j + 1, 1)
- .Cells((d.Count - 1) * 11 + 3, 4) = .Cells((d.Count - 1) * 11 + 3, 4) & Sheets(1).Cells(j + 1, 2)
- .Cells((d.Count - 1) * 11 + 3, 7) = .Cells((d.Count - 1) * 11 + 3, 7) & Sheets(1).Cells(j + 1, 3)
- .Cells((d.Count - 1) * 11 + 5, 1).Resize(1, 8).Value = Sheets(1).Cells(j + 1, 4).Resize(1, 8).Value
- d(CStr(arr1(j, 3))) = d(CStr(arr1(j, 3))) + 1
- Else
- .Cells(d(CStr(arr1(j, 3))), 1).Resize(1, 8).Value = Sheets(1).Cells(j + 1, 4).Resize(1, 8).Value
- d(CStr(arr1(j, 3))) = d(CStr(arr1(j, 3))) + 1
- End If
- End If
- Next j
- End With
- End Sub
复制代码 代码不够简洁,期待高手
|
|