ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 117|回复: 10

数据逐条按指定样式排版与判单数据插入行并统计数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-6-6 22:20 | 显示全部楼层 |阅读模式
求助;1,数据逐条按指定样式排版在新表向下排列方便打印。2.依据月份不同判断并插入空行并对指定列数据进行小计与总计计算(详见附件),烦请老师帮忙看看如何写代码。谢谢

送货单(带制作).zip

78.08 KB, 下载次数: 4

TA的精华主题

TA的得分主题

发表于 2024-6-7 08:03 | 显示全部楼层
  1. Sub test()
  2.     Dim r%, i%
  3.     Dim arr, brr
  4.     Dim d As Object
  5.     Set d = CreateObject("scripting.dictionary")
  6.     With Worksheets("送货单")
  7.         r = .Cells(.Rows.Count, 2).End(xlUp).Row
  8.         arr = .Range("a5:k" & r)
  9.     End With
  10.     For i = 1 To UBound(arr)
  11.         If Not d.exists(arr(i, 3)) Then
  12.             Set d(arr(i, 3)) = CreateObject("scripting.dictionary")
  13.         End If
  14.         d(arr(i, 3))(i) = Empty
  15.     Next
  16.     With Worksheets("送货单列表")
  17.         .Cells.Clear
  18.         r = 3
  19.         For Each aa In d.keys
  20.             ReDim crr(1 To d(aa).Count, 1 To 4)
  21.             m = d(aa).keys()(0)
  22.             brr = Application.Transpose(Application.Transpose(Array(Array("日期:", arr(m, 2), "客户:", arr(m, 8)), Array("单号:", arr(m, 3), "联系:", arr(m, 9)), Array("备注:", arr(m, 11), "地址:", arr(m, 10)))))
  23.             m = 0
  24.             For Each bb In d(aa).keys
  25.                 m = m + 1
  26.                 crr(m, 1) = arr(bb, 4)
  27.                 crr(m, 2) = arr(bb, 5)
  28.                 crr(m, 3) = arr(bb, 6)
  29.                 crr(m, 4) = arr(bb, 7)
  30.             Next
  31.             With .Cells(r, 2)
  32.                 .Value = "送货单"
  33.                 .Resize(1, 4).Merge
  34.             End With
  35.             .Cells(r + 1, 2).Resize(UBound(brr), UBound(brr, 2)) = brr
  36.             .Cells(r + 5, 2).Resize(1, 4) = Array("产品", "单价", "数量", "金额")
  37.             .Cells(r + 6, 2).Resize(UBound(crr), UBound(crr, 2)) = crr
  38.             .Cells(r + 6 + UBound(crr), 4) = "合计:"
  39.             With .Cells(r + 6 + UBound(crr), 5)
  40.                 .Value = Application.Sum(Application.Index(crr, 0, 4))
  41.                 .NumberFormatLocal = "¥#,##0.00_);[红色](¥#,##0.00)"
  42.                 .Font.ColorIndex = 3
  43.             End With
  44.             With .Cells(r + 1, 2).Resize(3, 4)
  45.                 With .Borders(xlEdgeTop)
  46.                     .LineStyle = xlContinuous
  47.                 End With
  48.                 With .Borders(xlEdgeBottom)
  49.                     .LineStyle = xlContinuous
  50.                 End With
  51.             End With
  52.             With .Cells(r + 6, 2).Resize(UBound(crr), 4)
  53.                 With .Borders(xlEdgeBottom)
  54.                     .LineStyle = xlDot
  55.                 End With
  56.                 With .Borders(xlInsideHorizontal)
  57.                     .LineStyle = xlDot
  58.                 End With
  59.             End With
  60.             With .Cells(r + 6, 3).Resize(UBound(crr), 1)
  61.                 .NumberFormatLocal = "¥#,##0.00_);[红色](¥#,##0.00)"
  62.             End With
  63.             With .Cells(r + 6, 5).Resize(UBound(crr), 1)
  64.                 .NumberFormatLocal = "¥#,##0.00_);[红色](¥#,##0.00)"
  65.             End With
  66.             r = r + 6 + UBound(crr) + 2
  67.         Next
  68.         With .UsedRange
  69.             With .Font
  70.                 .Name = "等线"
  71.                 .Size = 18
  72.             End With
  73.             .HorizontalAlignment = xlCenter
  74.             .VerticalAlignment = xlCenter
  75.         End With
  76.             
  77.     End With
  78. End Sub

复制代码

TA的精华主题

TA的得分主题

发表于 2024-6-7 08:04 | 显示全部楼层
只作了第一问。

送货单(带制作).rar

88.59 KB, 下载次数: 2

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-6-7 09:17 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
chxw68 发表于 2024-6-7 08:04
只作了第一问。

谢谢老师,可以。不愧是大神

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-6-7 11:11 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

老师好:我有疑难题请教,AI提问都没有结果,想来此请教:A1单元格中数据:买菜12.50元,搬货收入200块,卖1块香皂7.5块,打牌输-200元。我想用正则表达式提取正数并求和。12.50+200+7.5=220  请问老师如何写正则表达式。谢谢

TA的精华主题

TA的得分主题

发表于 2024-6-7 11:37 | 显示全部楼层
我对正则表达式也不是十分精通,参考吧。
  1. Sub test()
  2.     Dim r%, i%
  3.     Dim reg As New RegExp
  4.     With reg
  5.         .Global = True
  6.         .Pattern = "(-?(?:\d+\.\d+|\d+))[元块]"
  7.         ss = "买菜12.50元,搬货收入200块,卖1块香皂7.5块,打牌输-200元"
  8.         Set mh = .Execute(ss)
  9.         s = 0
  10.         For j = 0 To mh.Count - 1
  11.             x = Val(mh(j).SubMatches(0))
  12.             If x > 0 Then
  13.                 s = s + x
  14.             End If
  15.         Next
  16.         MsgBox s
  17.     End With
  18. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2024-6-7 14:29 | 显示全部楼层
Sub 删除小计总计()
Application.ScreenUpdating = False
Dim rn As Range
With ActiveSheet
    r = .Cells(Rows.Count, 3).End(xlUp).Row
    For i = 3 To r
        If .Cells(i, 3) = "小计" Or .Cells(i, 3) = "总计" Then
            If rn Is Nothing Then
                Set rn = .Rows(i)
            Else
                Set rn = Union(rn, .Rows(i))
            End If
        End If
    Next i
    If Not rn Is Nothing Then rn.Delete
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
Sub 插入小计总计()
Application.ScreenUpdating = False
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Dim rn As Range
With ActiveSheet
    r = .Cells(Rows.Count, 3).End(xlUp).Row
    ar = .Range("a1:e" & r)
    For i = 3 To UBound(ar)
        If ar(i, 2) <> "" Then
            If Not d.exists(ar(i, 2)) Then Set d(ar(i, 2)) = CreateObject("scripting.dictionary")
            d(ar(i, 2))(i) = ""
        End If
    Next i
    .Range("a3:e" & r) = Empty
    ReDim br(1 To UBound(ar) * 2, 1 To UBound(ar, 2))
    For Each k In d.keys
        hj1 = 0: hj2 = 0
        For Each kk In d(k).keys
            n = n + 1
            For j = 1 To UBound(ar, 2)
                br(n, j) = ar(kk, j)
            Next j
            hj1 = hj1 + ar(kk, 4)
            hj2 = hj2 + ar(kk, 5)
            zj1 = zj1 + ar(kk, 4)
            zj2 = zj2 + ar(kk, 5)
        Next kk
        n = n + 1
        br(n, 3) = "小计"
        br(n, 4) = hj1
        br(n, 5) = hj2
    Next k
    n = n + 1
    br(n, 3) = "总计"
    br(n, 4) = zj1
    br(n, 5) = zj2
    .[a3].Resize(n, UBound(br, 2)) = br
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub

TA的精华主题

TA的得分主题

发表于 2024-6-7 14:30 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Private Sub CommandButton1_Click()
With CommandButton1
    If .Caption = "插入小计" Then
        Call 插入小计总计
        .Caption = "删除小计"
    ElseIf .Caption = "删除小计" Then
        Call 删除小计总计
        .Caption = "插入小计"
    End If
End With
End Sub

TA的精华主题

TA的得分主题

发表于 2024-6-7 14:31 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
一个按钮两个功能
送货单(带制作).rar (56.64 KB, 下载次数: 3)

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-6-7 16:32 | 显示全部楼层
3190496160 发表于 2024-6-7 14:30
Private Sub CommandButton1_Click()
With CommandButton1
    If .Caption = "插入小计" Then

谢谢老师,完美,牛
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-20 23:15 , Processed in 0.048810 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表