一个同学让我帮下他的忙,写一个能生成工资单的Excel宏,从工资明细表中抽取相关数据,生成简易明了的工资单,尝试了一下,代码如下,仅作为记录:
- Sub 工资条计算()
- 'Sheet名称
- Dim DataSource As String
- Dim Target As String
- Dim Tpl As String
- Dim TableHeaderPos As Integer
- Dim EmptyCol As Integer
- Dim DataStartRow As Integer
- Dim MaxColCounts As Integer
- DataSource = "汇总明细"
- Target = "宏工资条"
- Tpl = "工资表1"
- TableHeaderPos = 4
- DataStartRow = TableHeaderPos + 1
- MaxColCounts = 32 '数据源中最大的横向宽度
- MaxColTplCounts = 16 '生成工资表中的最大横向宽度
- '收集工资单目标表头
- Dim TargetTableHeader(1 To 100) As String
- Dim Temp As Integer
- Temp = 1
- Do
- If (Worksheets(Tpl).Cells(1, Temp) = "" And Temp = MaxColTplCounts) Then Exit Do
- TargetTableHeader(Temp) = Worksheets(Tpl).Cells(1, Temp)
- Temp = Temp + 1
- Loop
- Temp = 1
- '得到总共的数据条数
- Dim AllDataCounts As Integer
- Do
- If (Worksheets(DataSource).Range("A" & Temp) = "") Then Exit Do
- Temp = Temp + 1
- Loop
- AllDataCounts = Temp - TableHeaderPos - 1
- '得到当前月份,工资单是上一个月
- Dim NowMonth As String
- Dim TableMonth As Integer
- NowMonth = Format(Now, "m")
- TableMonth = CInt(NowMonth) - 1
- '开始填充数据
- '外层循环,行数,Y
- Dim TargetDataStartRow As Integer
- Dim Cookie As Integer
- Cookie = 1
- TargetDataStartRow = 5 '默认从第5行开始
- For Y = TargetDataStartRow To (TargetDataStartRow + AllDataCounts - 1)
- '内层循环,列数,X
- For X = 1 To (MaxColTplCounts - 1)
- '写入表头
- Worksheets(Target).Cells(Y + Cookie - 1, X) = TargetTableHeader(X)
- '调整表头样式
- Worksheets(Target).Cells(Y + Cookie - 1, X).Select
- Selection.Font.Size = 10
- '写入数据
- '月份
- If (X = 1) Then Worksheets(Target).Cells(Y + Cookie, X) = TableMonth
- '姓名
- If (X = 2 Or X = 3) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X)
- '固定工资 9 + 10
- If (X = 4) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 9).Text) + Val(Worksheets(DataSource).Cells(Y, 10).Text)
- '绩效薪资标准,三个
- If (X = 5 Or X = 6 Or X = 7) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X + 6)
- '缺勤扣款
- If (X = 8) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, 15)
- '其他工资 16 + 17
- If (X = 9) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 16).Text) + Val(Worksheets(DataSource).Cells(Y, 17).Text)
- '福利收入 18 -> 22
- If (X = 10) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 18).Text) + Val(Worksheets(DataSource).Cells(Y, 19).Text) + Val(Worksheets(DataSource).Cells(Y, 20).Text) + Val(Worksheets(DataSource).Cells(Y, 21).Text) + Val(Worksheets(DataSource).Cells(Y, 22).Text)
- '其它及奖惩 23 - 24
- If (X = 11) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 23).Text) - Val(Worksheets(DataSource).Cells(Y, 24).Text)
- '应发工资 和 其他扣款
- If (X = 12 Or X = 13) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X + 13)
- '保险扣款 27 + 28 + 29
- If (X = 14) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 27).Text) + Val(Worksheets(DataSource).Cells(Y, 28).Text) + Val(Worksheets(DataSource).Cells(Y, 29).Text)
- '实发工资
- If (X = 15) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, MaxColCounts - 1)
- '调整样式
- Worksheets(Target).Cells(Y + Cookie, X).Select
- Selection.Font.Bold = True
- Next
- Cookie = Cookie + 1
- Next
- '数据生成完毕,开始样式调整
- '总体调整
- Cells.Select
- With Selection
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .WrapText = True
- .Orientation = 0
- .AddIndent = False
- .IndentLevel = 0
- .ShrinkToFit = False
- .ReadingOrder = xlContext
- .MergeCells = False
- End With
- Worksheets(Target).Range("A1").Select
- End Sub
今天(2012/07/29)又做了下修改,按照同学的一些改动需求:
- Sub 工资条计算()
- 'Sheet名称
- Dim DataSource As String
- Dim Target As String
- 'Dim Tpl As String
- Dim TableHeaderPos As Integer
- Dim EmptyCol As Integer
- Dim DataStartRow As Integer
- Dim MaxColCounts As Integer
- DataSource = "汇总明细"
- Target = "宏工资条"
- 'Tpl = "工资表1"
- TableHeaderPos = 4
- DataStartRow = TableHeaderPos + 1
- MaxColCounts = 32 '数据源中最大的横向宽度
- MaxColTplCounts = 16 '生成工资表中的最大横向宽度
- '收集工资单目标表头,写成死的表头
- Dim TargetTableHeader(1 To 100) As String
- '以下为注释
- 'Dim Temp As Integer
- 'Temp = 1
- 'Do
- ' If (Worksheets(Tpl).Cells(1, Temp) = "" And Temp = MaxColTplCounts) Then Exit Do
- ' TargetTableHeader(Temp) = Worksheets(Tpl).Cells(1, Temp)
- ' Temp = Temp + 1
- 'Loop
- TargetTableHeader(1) = "月份"
- TargetTableHeader(2) = "姓名"
- TargetTableHeader(3) = "中心/部门"
- TargetTableHeader(4) = "固定工资"
- TargetTableHeader(5) = "绩效薪资标准"
- TargetTableHeader(6) = "本月季绩效系数"
- TargetTableHeader(7) = "月季薪制绩效工资实发"
- TargetTableHeader(8) = "缺勤扣款"
- TargetTableHeader(9) = "其他工资"
- TargetTableHeader(10) = "福利收入"
- TargetTableHeader(11) = "其他及奖惩"
- TargetTableHeader(12) = "应发工资"
- TargetTableHeader(13) = "其他扣款"
- TargetTableHeader(14) = "保险扣款"
- TargetTableHeader(15) = "实发工资"
- Temp = 1
- '得到总共的数据条数
- Dim AllDataCounts As Integer
- Do
- If (Worksheets(DataSource).Range("A" & Temp) = "") Then Exit Do
- Temp = Temp + 1
- Loop
- AllDataCounts = Temp - TableHeaderPos - 1
- '得到当前月份,工资单是上一个月
- Dim NowMonth As String
- Dim TableMonth As Integer
- NowMonth = Format(Now, "m")
- TableMonth = CInt(NowMonth) - 1
- '开始填充数据
- '外层循环,行数,Y
- Dim TargetDataStartRow As Integer
- Dim Cookie As Integer
- Dim A As String
- Dim B As String
- Cookie = 1
- TargetDataStartRow = 5 '默认从第5行开始
- For Y = TargetDataStartRow To (TargetDataStartRow + AllDataCounts - 1)
- '内层循环,列数,X
- For X = 1 To (MaxColTplCounts - 1)
- '写入表头
- Worksheets(Target).Cells(Y + Cookie - 1, X) = TargetTableHeader(X)
- '写入数据
- '月份
- If (X = 1) Then Worksheets(Target).Cells(Y + Cookie, X) = TableMonth
- '姓名
- If (X = 2 Or X = 3) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X)
- '固定工资 9 + 10
- If (X = 4) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 9).Text) + Val(Worksheets(DataSource).Cells(Y, 10).Text)
- '绩效薪资标准,三个
- If (X = 5 Or X = 6 Or X = 7) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X + 6)
- '缺勤扣款
- If (X = 8) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, 15)
- '其他工资 16 + 17
- If (X = 9) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 16).Text) + Val(Worksheets(DataSource).Cells(Y, 17).Text)
- '福利收入 18 -> 22
- If (X = 10) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 18).Text) + Val(Worksheets(DataSource).Cells(Y, 19).Text) + Val(Worksheets(DataSource).Cells(Y, 20).Text) + Val(Worksheets(DataSource).Cells(Y, 21).Text) + Val(Worksheets(DataSource).Cells(Y, 22).Text)
- '其它及奖惩 23 - 24
- If (X = 11) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 23).Text) + Val(Worksheets(DataSource).Cells(Y, 24).Text)
- '应发工资 和 其他扣款
- If (X = 12 Or X = 13) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, X + 13)
- '保险扣款 27 + 28 + 29
- If (X = 14) Then Worksheets(Target).Cells(Y + Cookie, X) = Val(Worksheets(DataSource).Cells(Y, 27).Text) + Val(Worksheets(DataSource).Cells(Y, 28).Text) + Val(Worksheets(DataSource).Cells(Y, 29).Text)
- '实发工资
- If (X = 15) Then Worksheets(Target).Cells(Y + Cookie, X) = Worksheets(DataSource).Cells(Y, MaxColCounts - 1)
- Next
- '把调整样式的代码放在这里,执行效率比较高
- '表头,数据
- A = RTrim(LTrim(Str(Y + Cookie - 1)))
- B = RTrim(LTrim(Str(Y + Cookie)))
- '表头
- Worksheets(Target).Rows(A & ":" & A).Select
- Selection.Font.Size = 10
- Selection.RowHeight = 24
- '数据
- Worksheets(Target).Rows(B & ":" & B).Select
- Selection.Font.Size = 11
- Selection.RowHeight = 24
- Selection.Font.Bold = True
- Cookie = Cookie + 1
- Next
- '数据生成完毕,开始样式调整
- '总体调整
- Cells.Select
- With Selection
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .WrapText = True
- .Orientation = 0
- .AddIndent = False
- .IndentLevel = 0
- .ShrinkToFit = False
- .ReadingOrder = xlContext
- .MergeCells = False
- End With
- Worksheets(Target).Range("A1").Select
- End Sub
新闻热点
疑难解答