首页 > 编程语言 > 详细

EXCEL的VBA(宏)编程

时间:2019-12-09 20:11:20      阅读:109      评论:0      收藏:0      [点我收藏+]

EXCEL的VBA编程

杨康需要我完成的需求

第一列是名称

第二列是甲方账户

第三列是甲方金额

第四列是乙方账户

第五列是乙方金额

第六列是true或false

第七列备注
技术分享图片
需求

开始时数据对齐的,如果甲方金额比乙方金额大,就将交易名称,甲方账户,甲方金额下移一行,反之则将乙方账户,乙方金额下移一行

最终效果图
技术分享图片

我的代码


Sub func()

Dim pos, total, mate1

Rem 获取行数
total = Sheet1.UsedRange.Rows.Count

Rem 开始循环
For pos = 2 To 66666    '66666是设置的一个极大值,实际上,当不存在交易的时候会自动跳出for循环,不会循环这么多次
    If Sheet1.Cells(pos, "C").Value > Sheet1.Cells(pos, "E").Value Then     '当甲的金额大于乙的金额
       MsgBox "甲大!!"
       Sheet1.Cells(pos, "G") = "无甲方"
       Sheet1.Cells(pos, "F") = "FALSE"          '改为FLASE
       Range("A" & pos & ":C" & total).Select
       Selection.Copy Range("A" & pos + 1)        '移动
       Range("A" & pos & ":C" & pos) = ""
       Range("A" & pos & ":G" & pos).Select   '选中当前的行
       Set rng2 = ActiveCell.EntireRow
       rng2.Interior.ColorIndex = 6             '设置高亮
    ElseIf Sheet1.Cells(pos, "C").Value < Sheet1.Cells(pos, "E").Value Then     '当甲的金额小于乙的金额
       MsgBox "乙大!!"
       Sheet1.Cells(pos, "G") = "无乙方"
       Sheet1.Cells(pos, "F") = "FALSE"
       Range("D" & pos & ":E" & total).Select
       Selection.Copy Range("D" & pos + 1)
       Range("D" & pos & ":E" & pos) = ""
       Range("A" & pos & ":G" & pos).Select   '选中当前的行
       Set rng2 = ActiveCell.EntireRow
       rng2.Interior.ColorIndex = 6             '设置高亮
    ElseIf Sheet1.Cells(pos, "C").Value = Sheet1.Cells(pos, "E").Value And Sheet1.Cells(pos, "A").Value <> "" Then '如果甲乙账户金额相等
    Sheet1.Cells(pos, "F") = "TRUE"
    Else            '否则就跳出循环
        Exit For
    End If
Next
MsgBox "完成!!"
End Sub

效果图
技术分享图片

EXCEL的VBA(宏)编程

原文:https://www.cnblogs.com/realwuxiong/p/12012814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!