美文网首页
【Excel VBA】2018-09-29 自动开送货单

【Excel VBA】2018-09-29 自动开送货单

作者: Ravlee | 来源:发表于2018-09-30 09:18 被阅读0次

案例

案例来源:Excel和Access (微信公众号)点击 - 查看原文

案例示图

如上图,根据输入的名称、单位、数量、单价等信息,自动计算金额。

附件:点击查看-百度云
提取密码:ehxp


1、数据源代码

Sub 源数据代码()
    Dim i As Integer
    '录入原始数据
    Cells(1, 1) = "送货单"
    Cells(2, 1) = "收货地址:"
    Cells(2, 6) = "No."
    Cells(3, 1) = "商家地址:"
    Cells(3, 6) = "日期:"
    Cells(4, 1) = "序号"
    Cells(4, 2) = "名称及规格"
    Cells(4, 3) = "单位"
    Cells(4, 4) = "数量"
    Cells(4, 5) = "单价"
    Cells(4, 6) = "金        额"
    Cells(5, 6) = "十"
    Cells(5, 7) = "万"
    Cells(5, 8) = "仟"
    Cells(5, 9) = "佰"
    Cells(5, 10) = "十"
    Cells(5, 11) = "元"
    Cells(5, 12) = "角"
    Cells(5, 13) = "分"
    For i = 1 To 8
        Cells(5 + i, 1) = i
    Next
    Cells(14, 1) = "大写:"
    Cells(14, 5) = "合计:"
    Cells(15, 1) = "电话/传真:"
    Cells(16, 1) = "收货人:"
    Cells(16, 5) = "送货人:"
    
    '整理格式
    Range("a1:m1").Merge
    Range("g2:j2").Merge
    Range("g3:j3").Merge
    Range("a4:a5").Merge
    Range("b4:b5").Merge
    Range("c4:c5").Merge
    Range("d4:d5").Merge
    Range("e4:e5").Merge
    Range("f4:m4").Merge
    Range("b14:d14").Merge
    Range("f14:m14").Merge
    Range("b15:m15").Merge
    Range("a4:m15").Borders.LineStyle = 1
    With Range("a1")
        .HorizontalAlignment = xlCenter
        .Font.Size = 18
        .Font.Bold = True
    End With
    Range("f2:f3").HorizontalAlignment = xlCenter
    Range("a4:m5").HorizontalAlignment = xlCenter
    Range("a6:a15").HorizontalAlignment = xlCenter
    
    Columns("a:m").AutoFit
    ActiveWindow.DisplayGridlines = False '设置背景网格线

End Sub
  • Merge 合并单元格
    写法格式:Range("单元格区域").Merge

  • HorizontalAlignment 设置水平对齐
    写法格式:Range("单元格区域").HorizontalAlignment = xlCenter,示例为水平居中

  • 工作表网格线
    写法格式:ActiveWindow.DisplayGridlines = False,设置隐藏工作表网格线。默认值为True


2、金额合计

Sub 金额合计()
Dim i, k As Integer
Dim s As Currency

For i = 6 To 13
'循环遍历,填入的内容
    If Range("b" & i) <> "" And Range("d" & i) <> "" And Range("e" & i) <> "" Then
    '判断填入的内容不为空
        For k = 1 To Len(Range("d" & i) * Range("e" & i) * 100)
        'k表示计算后的金额,需要循环的次数。对应填入右侧的金额栏内
        Cells(i, 13 + k - Len(Range("d" & i) * Range("e" & i) * 100)) = Mid(Range("d" & i) * Range("e" & i) * 100, k, 1)
        'cells(行号,列号)
        '金额为100时,当k=1时,13+1-5=9,提取第1位;当k=2时,13+2-5=10,提取第2位;当k=5时,13+5-5=13,提取第5位
        '使用cells,是因为可以改变列,好于Range方法
        Next
    End If
s = s + Range("d" & i) * Range("e" & i)
Next
Range("b14,f14") = s
Range("b14").NumberFormatLocal = "[DBNum2][$-zh-CN]G/通用格式"
End Sub

思考解决方案

2.1 通过循环函数,每次仅处理一行的数据;
2.2 通过if函数,判断一行数据的内容是否完整。如果完整,则计算数量*单价的金额。 乘100表示去掉小数点,便于Len函数统计金额的位数;
2.3 通过Len获取的位数,通过for循环和Mid提取函数,把每一位数字提取并填入到对应的单元格里。Cells(i, 13 + k - Len(Range("d" & i) * Range("e" & i) * 100)) = Mid(Range("d" & i) * Range("e" & i) * 100, k, 1)
2.4 通过Range("单元格对象").NumberFormatLocal="[DBNum2][$-zh-cn]G/通用格式"设定金额大写的格式。


3、重新开单

Sub 重新开单()
    Range("b2:b3").ClearContents
    Range("g2:g3") = ""
    Range("b15") = ""
    Range("b16").ClearContents
    Range("f16").ClearContents
    Range("b6:n13").ClearContents
    
    Range("f14") = ""
    Range("g2") = Now * 100000
    Range("g2").NumberFormatLocal = "G/通用格式"
    Range("g3") = Date
End Sub

3.1 ClearContents用于清除内容,但保留格式;
3.2 range("单元格对象")=date,对单元格赋值为日期。

相关文章

网友评论

      本文标题:【Excel VBA】2018-09-29 自动开送货单

      本文链接:https://www.haomeiwen.com/subject/vyepoftx.html