Excel填充数据

时间:2017-03-31 03:43:37

标签: excel excel-vba vba

我有以下输入数据:

enter image description here

我需要将每个条目填充到3行中,并且支付率不同&期间,像这样:

enter image description here

佣金支付基于以下比率:
i)第1个月 - 50%
ii)第2个月 - 30%
iii)第3个月 - 20%

实施例: -

enter image description here

下一个标准是佣金支付期。本月收集将在接下来的几个月内支付。

我怎样才能使用Excel&宏?

感谢。

1 个答案:

答案 0 :(得分:1)

假设您的数据在第一张纸上,请创建第二张纸并运行此宏:

Option Explicit

Sub popData()

 Dim r As Integer, r2 As Integer, p As Integer: r2 = 3
 Dim sht2 As Worksheet: Set sht2 = Sheets(2)

 Dim rate(), headings()
 rate = Array(0.5, 0.3, 0.2)
 headings = Array("Date", "Sales", "Commission", "Collection Date", "Invoice No")

 For r = 0 To UBound(headings):
   sht2.Cells(2, r + 1).Value = headings(r)
 Next

 Dim dat As Date
 With Sheets(1)
   sht2.Cells(1, 1).Value = "Payout"
   For r = 3 To .Cells(.Rows.Count, "A").End(xlUp).row:
     For p = 0 To 2:
       sht2.Cells(r2, "A").NumberFormat = .Cells(r, "A").NumberFormat
       dat = DateAdd("m", p + 1, CDate(.Cells(r, "A")))
       sht2.Cells(r2, "A").Value = DateSerial(year(dat), month(dat) + 1, 0)
       sht2.Cells(r2, "B").Value = .Cells(r, "B").Value
       sht2.Cells(r2, "C").NumberFormat = "0.00"
       sht2.Cells(r2, "C").Value = .Cells(r, "C").Value * rate(p)
       sht2.Cells(r2, "D").NumberFormat = .Cells(r, "A").NumberFormat
       sht2.Cells(r2, "D").Value = .Cells(r, "A").Value
       sht2.Cells(r2, "E").Value = .Cells(r, "D").Value
       r2 = r2 + 1
     Next
   Next
 End With

 sht2.Columns("A:E").EntireColumn.AutoFit

End Sub

第一个循环将标题添加到第二个工作表。下一个双循环使用内部循环处理每个销售人员,为每个佣金支付创建3行。您似乎想要下个月的最后一天 - 这是通过在当前日期添加一个月然后使用DateSerial函数来查找该月的最后一天来提供的。最后一行只是确保所有列都扩展到最大的文本条目,因此一切都可见。