|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 tao60 于 2019-7-22 09:09 编辑
好久没写VBA了,很多代码都不记得了,很多代码都是通过录制宏生成的,然后再进行精简和修改。
功能基本实现了,细节功能,可以自己研究一下。
- 'tao60 2019-07-18
- Sub AllData()
- '开始批处理
- Dim iRow As Integer
- Dim iRowBegin As Integer
- Dim iRowEnd As Integer
- Dim iRow_all As Integer
- Dim strUser As String
- Dim stemp As String
- Dim strPath As String
-
-
- Dim shtList As Object
- Dim shtOut As Object
- Set shtList = Sheets("列表")
- Set shtOut = Sheets("模板")
-
- '选择保存路径
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "选择要保存的路径……"
- If .Show = False Then
- Exit Sub
- End If
-
- strPath = .SelectedItems(1)
- End With
-
- '列表最大行号
- iRow_all = shtList.Range("A65535").End(xlUp).Row
-
- iRowBegin = 2
- strUser = Trim(shtList.Cells(2, 4))
-
- '从列表开始行号2开始处理
- For iRow = 2 To iRow_all Step 1
- stemp = Trim(shtList.Cells(iRow, 4))
-
- If strUser <> stemp Or iRow = iRow_all Then
- iRowEnd = iRow - 1
- '处理最后一行2019-07-22
- If iRow = iRow_all Then
- iRowEnd = iRow_all
- End If
- '处理同一个收款人的数据,保存到一个文件,请先对列表数据进行排序,按照收款人进行排序
- Call PathData(iRowBegin, iRowEnd, strUser, strPath)
-
- iRowBegin = iRow
- strUser = shtList.Cells(iRow, 4)
- End If
- Next
- End Sub
- Function PathData(iBegin As Integer, iEnd As Integer, sName As String, sPath As String)
- '子程序 根据列表起止行号将列表数据写入到模板中,并另外文件和打印
- 'iBegin 开始行号, iEnd 结束行号, sName 文件名, sPath 另存文件路径
- If iBegin = 0 Or iEnd = 0 Or iBegin > iEnd Then
- Exit Function
- End If
-
- Dim shtList As Object
- Dim shtOut As Object
- Set shtList = Sheets("列表")
- Set shtOut = Sheets("模板")
-
- shtOut.Rows("4:65535").Delete Shift:=xlUp '删除所有数据行
-
- Dim iRowList As Integer
- Dim iRowOut As Integer
- Dim iCount As Integer
-
- iCount = 1
- iRowOut = 4 '模板用于填写数据的位置行号
- For iRowList = iBegin To iEnd Step 1
- shtOut.Range("A" & iRowOut).Value = iCount
- shtOut.Range("B" & iRowOut).Value = "'" & shtList.Range("B" & iRowList).Value
- shtOut.Range("C" & iRowOut).Value = "'" & shtList.Range("C" & iRowList).Value
- shtOut.Range("D" & iRowOut).Value = shtList.Range("H" & iRowList).Value
- shtOut.Range("E" & iRowOut).Value = shtList.Range("I" & iRowList).Value
- shtOut.Range("F" & iRowOut).Value = shtList.Range("J" & iRowList).Value
- shtOut.Range("G" & iRowOut).FormulaR1C1 = "=RC[-3]-RC[-2]"
- shtOut.Range("H" & iRowOut).Value = "'" & shtList.Range("D" & iRowList).Value
- shtOut.Range("I" & iRowOut).Value = "'" & shtList.Range("E" & iRowList).Value
- shtOut.Range("J" & iRowOut).Value = "'" & shtList.Range("F" & iRowList).Value
- iCount = iCount + 1
- iRowOut = iRowOut + 1
- Next
-
- '汇总行
- shtOut.Range("A" & iRowOut).Value = "合计"
- shtOut.Range("A" & iRowOut & ":J" & iRowOut).Font.Bold = True
- shtOut.Range("D" & iRowOut).FormulaR1C1 = "=SUM(R[-" & iCount - 1 & "]C:R[-1]C)"
- shtOut.Range("E" & iRowOut).FormulaR1C1 = "=SUM(R[-" & iCount - 1 & "]C:R[-1]C)"
- shtOut.Range("F" & iRowOut).FormulaR1C1 = "=SUM(R[-" & iCount - 1 & "]C:R[-1]C)"
- shtOut.Range("G" & iRowOut).FormulaR1C1 = "=SUM(R[-" & iCount - 1 & "]C:R[-1]C)"
- '边框线
- shtOut.Range("A4:J" & iRowOut).Borders(xlEdgeLeft).LineStyle = xlContinuous
- shtOut.Range("A4:J" & iRowOut).Borders(xlEdgeTop).LineStyle = xlContinuous
- shtOut.Range("A4:J" & iRowOut).Borders(xlEdgeBottom).LineStyle = xlContinuous
- shtOut.Range("A4:J" & iRowOut).Borders(xlEdgeRight).LineStyle = xlContinuous
- shtOut.Range("A4:J" & iRowOut).Borders(xlInsideVertical).LineStyle = xlContinuous
- shtOut.Range("A4:J" & iRowOut).Borders(xlInsideHorizontal).LineStyle = xlContinuous
-
- '字体
- shtOut.Range("A4:J" & iRowOut).Font.Name = "宋体"
- shtOut.Range("A4:J" & iRowOut).Font.Size = 10
- shtOut.Range("A4:A" & iRowOut).HorizontalAlignment = xlCenter
-
- '底部签名栏
- shtOut.Range("A" & iRowOut + 3).Value = "分公司负责人: 财务负责人: 业管部负责人: 渠道部负责人: 财务复核:"
- shtOut.Range("A" & iRowOut + 6).Value = "代理人: 制表:"
- shtOut.Range("A" & iRowOut + 9).Value = "总公司分管会计:"
-
- '另存文件
- Dim sFileName As String
- Dim wbOut As Object
-
- sFileName = sPath & "" & sName & ".xlsx"
- shtOut.Copy
- Set wbOut = ActiveWorkbook
- wbOut.ActiveSheet.Name = sName
- wbOut.SaveAs Filename:=sFileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges
-
- '直接打印 无预览
- wbOut.ActiveSheet.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
-
- '关闭
- wbOut.Close False
- End Function
- Function test()
- 'DEMO测试
- Call PathData(2, 5, "四四", "D:\2345Downloads")
-
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|