ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

本人在学习excel vba,希望用宏完成自动取数的填写社保

[复制链接]

TA的精华主题

TA的得分主题

发表于 2014-4-24 10:42 | 显示全部楼层 |阅读模式
社保.rar (116.49 KB, 下载次数: 43) 本人用if函数实现了大部分情况可以用的自动填写社保进社保审核明细表,现在想用vba完成这一工作,顺便学习下vba,请高手指点下,希望把1.xls的表里面的数据填进社保审核明细表对应的地方,比如1。xls 就填进1月,养老保险就填进养老保险,一般来说社保审核明细表是不会变的,每个月的社保格式一般也不会变,但它有时会缺少一个两个险种,或多一两个险种,也就是说它里面的列数是会变动的,人数会变动。

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-5-28 17:11 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub 社保()
  2.     For l = 1 To 12 Step 1
  3.     Workbooks.Open Filename:="F:\工作1\宏\社保" & l & ".xls"
  4. '文件路径根据需要修改
  5.     Range("L4").Select
  6.     ActiveCell.FormulaR1C1 = "1"
  7.     Range("L4").Select
  8.     Selection.Copy
  9.     Range("E9:BJ452").Select
  10.     Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
  11.         SkipBlanks:=False, Transpose:=False
  12. '上述代码用于文本转数字
  13.     For i = 4 To 30 Step 1
  14.    If Cells(7, i) = "基本养老保险(非本市城镇户籍)" Then If Cells(8, i + 1) = "单位" Then Cells(3, 79) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  15.    
  16.    Next i
  17.     For i = 4 To 30 Step 1
  18.    If Cells(7, i) = "基本养老保险(非本市城镇户籍)" Then If Cells(8, i + 2) = "个人" Then Cells(3, 80) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  19.    
  20.    Next i
  21.    
  22.     For i = 4 To 30 Step 1
  23.    If Cells(7, i) = "基本养老保险" Then If Cells(8, i + 1) = "单位" Then Cells(4, 79) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  24.    
  25.    Next i
  26.     For i = 4 To 30 Step 1
  27.    If Cells(7, i) = "基本养老保险" Then If Cells(8, i + 2) = "个人" Then Cells(4, 80) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  28.    
  29.    Next i
  30.    
  31.    
  32.     For i = 4 To 30 Step 1
  33.    If Cells(7, i) = "基本医疗保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 81) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  34.    
  35.    Next i
  36.     For i = 4 To 30 Step 1
  37.    If Cells(7, i) = "基本医疗保险" Then If Cells(8, i + 2) = "个人" Then Cells(3, 82) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  38.    
  39.    Next i
  40.     For i = 4 To 30 Step 1
  41.    If Cells(7, i) = "工伤保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 85) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  42.    
  43.    Next i
  44.     For i = 4 To 30 Step 1
  45.    If Cells(7, i) = "生育保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 86) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  46.    
  47.    Next i
  48.     For i = 4 To 30 Step 1
  49.    If Cells(7, i) = "重大疾病医疗补助" Then If Cells(8, i + 1) = "单位" Then Cells(3, 87) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  50.    
  51.    Next i
  52.     For i = 4 To 30 Step 1
  53.    If Cells(7, i) = "失业保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 83) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  54.    
  55.    Next i
  56.     For i = 4 To 30 Step 1
  57.    If Cells(7, i) = "失业保险" Then If Cells(8, i + 2) = "个人" Then Cells(3, 84) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  58.        Next i
  59.    For i = 4 To 30 Step 1
  60.    If Cells(7, i) = "城镇职工补充医疗保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 88) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  61.    
  62.    Next i
  63.    For i = 4 To 30 Step 1
  64.    If Cells(7, i) = "失业保险(农民工)" Then If Cells(8, i + 1) = "单位" Then Cells(4, 83) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  65.    
  66.    Next i
  67.    
  68.    Next l
  69.    For p = 1 To 12 Step 1
  70.    Windows("" & p & ".XLS").Activate
  71.     Range("CA3:CJ3").Select
  72.     Selection.Copy
  73.     Windows("社保明细审核表.xls").Activate
  74.     Range("B" & p + 8).Select
  75.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  76.         :=False, Transpose:=False
  77.     Windows("" & p & ".XLS").Activate
  78.     Range("CA4:CJ4").Select
  79.     Application.CutCopyMode = False
  80.     Selection.Copy
  81.     Windows("社保明细审核表.xls").Activate
  82.     Range("B" & p + 8).Select
  83.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks _
  84.         :=False, Transpose:=False
  85.         Next p
  86.         Workbooks.Close
  87. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-5-29 12:18 | 显示全部楼层
本帖最后由 燠风 于 2014-5-29 15:18 编辑
  1. 社保明细审核表.rar (16.91 KB, 下载次数: 18) Sub 社保1()
  2.     Dim FileName As String
  3.     Dim r As Integer
  4.     FileName = Dir(ThisWorkbook.Path & "\*.xls")
  5.     Do While FileName <> ""
  6.      If FileName <> ThisWorkbook.Name Then  k = ThisWorkbook.Path
  7.      Workbooks.Open k & "" & FileName
  8.     r = Left(FileName, 2)
  9.     Range("L4").Select
  10.     ActiveCell.FormulaR1C1 = "1"
  11.     Range("L4").Select
  12.     Selection.Copy
  13.     Range("E9:BJ452").Select
  14.     Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
  15.         SkipBlanks:=False, Transpose:=False
  16.     For i = 4 To 30 Step 1
  17.    If Cells(7, i) = "基本养老保险(非本市城镇户籍)" Then If Cells(8, i + 1) = "单位" Then Cells(3, 79) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  18.    
  19.    Next i
  20.     For i = 4 To 30 Step 1
  21.    If Cells(7, i) = "基本养老保险(非本市城镇户籍)" Then If Cells(8, i + 2) = "个人" Then Cells(3, 80) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  22.       Next i
  23.       For i = 4 To 30 Step 1
  24.    If Cells(7, i) = "基本养老保险" Then If Cells(8, i + 1) = "单位" Then Cells(4, 79) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  25.       Next i
  26.     For i = 4 To 30 Step 1
  27.    If Cells(7, i) = "基本养老保险" Then If Cells(8, i + 2) = "个人" Then Cells(4, 80) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  28.     Next i
  29.      For i = 4 To 30 Step 1
  30.    If Cells(7, i) = "基本医疗保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 81) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  31.       Next i
  32.     For i = 4 To 30 Step 1
  33.    If Cells(7, i) = "基本医疗保险" Then If Cells(8, i + 2) = "个人" Then Cells(3, 82) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  34.      Next i
  35.     For i = 4 To 30 Step 1
  36.    If Cells(7, i) = "工伤保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 85) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  37.     Next i
  38.     For i = 4 To 30 Step 1
  39.    If Cells(7, i) = "生育保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 86) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  40.      Next i
  41.     For i = 4 To 30 Step 1
  42.    If Cells(7, i) = "重大疾病医疗补助" Then If Cells(8, i + 1) = "单位" Then Cells(3, 87) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  43.      Next i
  44.     For i = 4 To 30 Step 1
  45.    If Cells(7, i) = "失业保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 83) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  46.    
  47.    Next i
  48.     For i = 4 To 30 Step 1
  49.    If Cells(7, i) = "失业保险" Then If Cells(8, i + 2) = "个人" Then Cells(3, 84) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 2), Cells(300, i + 2)))
  50.        Next i
  51.    For i = 4 To 30 Step 1
  52.    If Cells(7, i) = "城镇职工补充医疗保险" Then If Cells(8, i + 1) = "单位" Then Cells(3, 88) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  53.    
  54.    Next i
  55.    For i = 4 To 30 Step 1
  56.    If Cells(7, i) = "过渡性基本医疗保险金" Then If Cells(8, i + 1) = "单位" Then Cells(4, 88) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  57.    
  58.    Next i
  59.    For i = 4 To 30 Step 1
  60.    If Cells(7, i) = "失业保险(农民工)" Then If Cells(8, i + 1) = "单位" Then Cells(4, 83) = Application.WorksheetFunction.Sum(Range(Cells(9, i + 1), Cells(300, i + 1)))
  61.    
  62.    Next i
  63.     Windows("" & r & ".XLS").Activate
  64.     Range("CA3:CJ3").Select
  65.     Selection.Copy
  66.     Windows("社保明细审核表.xls").Activate
  67.     Range("B" & r + 8).Select
  68.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  69.         :=False, Transpose:=False
  70.     Windows("" & r & ".XLS").Activate
  71.     Range("CA4:CJ4").Select
  72.     Application.CutCopyMode = False
  73.     Selection.Copy
  74.     Windows("社保明细审核表.xls").Activate
  75.     Range("B" & r + 8).Select
  76.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks _
  77.         :=False, Transpose:=False
  78.   End If
  79.         FileName = Dir    ' 用Dir 函数取得其他文件名,并赋给变量
  80.    Loop
  81.    Workbooks.Close
  82. End Sub


复制代码
楼上的当文件少了1个就会出现下标越界,比楼上好在,如果不是12个文件也可以运行,可以不用改文件路径名了,放在其他地方也可以用,但是该文件夹要只有这些文件,如果有其他不相干的文件就会出错
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-11-23 04:22 , Processed in 0.040173 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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