1. '1.用户可以任意选择文件夹进行遍历
  2. '2.限定遍历时仅搜索EXCEL文件(你可以改变文件类型)
  3. '这个程序要先在“引用”下选择"microsoft scripting runtime"库文件
  4.  
  5. Dim ArryFile() As String
  6. Dim nFile As Integer
  7. Sub Filelist()
  8. Dim fso As New FileSystemObject
  9. Dim fd As Folder
  10. Dim strFilePath As String
  11. Dim FolderSelect As FileDialog
  12. Set FolderSelect = Application.FileDialog(msoFileDialogFolderPicker)
  13. With FolderSelect
  14. If .Show = -1 Then
  15. strFilePath = .SelectedItems.Item(1) & "\"
  16. End If
  17. End With
  18. Set fd = fso.GetFolder(strFilePath)
  19. nFile = 0
  20. searchFile fd
  21. End Sub
  22.  
  23. Private Function searchFile(ByVal fd As Folder)
  24. Dim fl As File
  25. Dim subfd As Folder
  26. Dim i As Integer
  27. On Error Resume Next
  28.  
  29. i = fd.files.Count
  30.  
  31. ReDim Preserve ArryFile(1 To nFile + i)
  32. For Each fl In fd.files
  33. If Right(fl.Name, 4) = "xlsx" Then '后缀是xls的用 If Right(fl.Name, 3) = "xls" Then
  34. nFile = nFile + 1
  35. ArryFile(nFile) = fl.Path
  36. End If
  37. Next
  38. If fd.SubFolders.Count = 0 Then Exit Function
  39. For Each subfd In fd.SubFolders
  40. searchFile subfd
  41. Next
  42. End Function
  43.  
  44. //主函数,运行时调用该函数
  45. Sub ttt1()
  46.  
  47. Dim xlname, myxl As Object, sh As Object
  48.  
  49. Call Filelist
  50.  
  51. 'Set myxl = CreateObject("Aplication.Excel")
  52.  
  53. If nFile > 0 Then
  54.  
  55. For Each xlname In ArryFile()
  56. If xlname <> "" Then
  57. //打开
  58. Workbooks.Open Filename:=xlname
  59. //调用Excel处理函数
  60. Call Macro3
  61. //保存,关闭
  62. ActiveWorkbook.Save
  63. ActiveWorkbook.Close
  64. End If
  65. Next
  66. End If
  67.  
  68. Set myxl = Nothing
  69. End Sub
  70.  
  71. //Excel处理函数,该段替换成自己的处理过程
  72. Sub Macro3()
  73. '
  74. ' Macro3 Macro
  75. '
  76. ' 快捷键: Ctrl+Shift+C
  77. '
  78. Range("V3:X3").Select
  79. ActiveCell.FormulaR1C1 = "/"
  80. With ActiveCell.Characters(Start:=1, Length:=1).Font
  81. .Name = "宋体"
  82. .FontStyle = "常规"
  83. .Size = 10
  84. .Strikethrough = False
  85. .Superscript = False
  86. .Subscript = False
  87. .OutlineFont = False
  88. .Shadow = False
  89. .Underline = xlUnderlineStyleNone
  90. .ColorIndex = 1
  91. .TintAndShade = 0
  92. .ThemeFont = xlThemeFontNone
  93. End With
  94. Range("B5:J5").Select
  95. ActiveCell.FormulaR1C1 = "R种植业 □林业 □畜牧业 □渔业 □其他 "
  96. With ActiveCell.Characters(Start:=1, Length:=1).Font
  97. .Name = "Wingdings 2"
  98. .FontStyle = "常规"
  99. .Size = 10
  100. .Strikethrough = False
  101. .Superscript = False
  102. .Subscript = False
  103. .OutlineFont = False
  104. .Shadow = False
  105. .Underline = xlUnderlineStyleNone
  106. .ColorIndex = 1
  107. .TintAndShade = 0
  108. .ThemeFont = xlThemeFontNone
  109. End With
  110. With ActiveCell.Characters(Start:=2, Length:=3).Font
  111. .Name = "宋体"
  112. .FontStyle = "常规"
  113. .Size = 10
  114. .Strikethrough = False
  115. .Superscript = False
  116. .Subscript = False
  117. .OutlineFont = False
  118. .Shadow = False
  119. .Underline = xlUnderlineStyleNone
  120. .ColorIndex = 1
  121. .TintAndShade = 0
  122. .ThemeFont = xlThemeFontNone
  123. End With
  124. With ActiveCell.Characters(Start:=5, Length:=2).Font
  125. .Name = "Wingdings 2"
  126. .FontStyle = "常规"
  127. .Size = 10
  128. .Strikethrough = False
  129. .Superscript = False
  130. .Subscript = False
  131. .OutlineFont = False
  132. .Shadow = False
  133. .Underline = xlUnderlineStyleNone
  134. .ColorIndex = 1
  135. .TintAndShade = 0
  136. .ThemeFont = xlThemeFontNone
  137. End With
  138. With ActiveCell.Characters(Start:=7, Length:=3).Font
  139. .Name = "宋体"
  140. .FontStyle = "常规"
  141. .Size = 10
  142. .Strikethrough = False
  143. .Superscript = False
  144. .Subscript = False
  145. .OutlineFont = False
  146. .Shadow = False
  147. .Underline = xlUnderlineStyleNone
  148. .ColorIndex = 1
  149. .TintAndShade = 0
  150. .ThemeFont = xlThemeFontNone
  151. End With
  152. With ActiveCell.Characters(Start:=10, Length:=2).Font
  153. .Name = "Wingdings 2"
  154. .FontStyle = "常规"
  155. .Size = 10
  156. .Strikethrough = False
  157. .Superscript = False
  158. .Subscript = False
  159. .OutlineFont = False
  160. .Shadow = False
  161. .Underline = xlUnderlineStyleNone
  162. .ColorIndex = 1
  163. .TintAndShade = 0
  164. .ThemeFont = xlThemeFontNone
  165. End With
  166. With ActiveCell.Characters(Start:=12, Length:=4).Font
  167. .Name = "宋体"
  168. .FontStyle = "常规"
  169. .Size = 10
  170. .Strikethrough = False
  171. .Superscript = False
  172. .Subscript = False
  173. .OutlineFont = False
  174. .Shadow = False
  175. .Underline = xlUnderlineStyleNone
  176. .ColorIndex = 1
  177. .TintAndShade = 0
  178. .ThemeFont = xlThemeFontNone
  179. End With
  180. With ActiveCell.Characters(Start:=16, Length:=4).Font
  181. .Name = "Wingdings 2"
  182. .FontStyle = "常规"
  183. .Size = 10
  184. .Strikethrough = False
  185. .Superscript = False
  186. .Subscript = False
  187. .OutlineFont = False
  188. .Shadow = False
  189. .Underline = xlUnderlineStyleNone
  190. .ColorIndex = 1
  191. .TintAndShade = 0
  192. .ThemeFont = xlThemeFontNone
  193. End With
  194. With ActiveCell.Characters(Start:=20, Length:=3).Font
  195. .Name = "宋体"
  196. .FontStyle = "常规"
  197. .Size = 10
  198. .Strikethrough = False
  199. .Superscript = False
  200. .Subscript = False
  201. .OutlineFont = False
  202. .Shadow = False
  203. .Underline = xlUnderlineStyleNone
  204. .ColorIndex = 1
  205. .TintAndShade = 0
  206. .ThemeFont = xlThemeFontNone
  207. End With
  208. With ActiveCell.Characters(Start:=23, Length:=4).Font
  209. .Name = "Wingdings 2"
  210. .FontStyle = "常规"
  211. .Size = 10
  212. .Strikethrough = False
  213. .Superscript = False
  214. .Subscript = False
  215. .OutlineFont = False
  216. .Shadow = False
  217. .Underline = xlUnderlineStyleNone
  218. .ColorIndex = 1
  219. .TintAndShade = 0
  220. .ThemeFont = xlThemeFontNone
  221. End With
  222. With ActiveCell.Characters(Start:=27, Length:=3).Font
  223. .Name = "宋体"
  224. .FontStyle = "常规"
  225. .Size = 10
  226. .Strikethrough = False
  227. .Superscript = False
  228. .Subscript = False
  229. .OutlineFont = False
  230. .Shadow = False
  231. .Underline = xlUnderlineStyleNone
  232. .ColorIndex = 1
  233. .TintAndShade = 0
  234. .ThemeFont = xlThemeFontNone
  235. End With
  236. With ActiveCell.Characters(Start:=30, Length:=1).Font
  237. .Name = "Wingdings 2"
  238. .FontStyle = "常规"
  239. .Size = 10
  240. .Strikethrough = False
  241. .Superscript = False
  242. .Subscript = False
  243. .OutlineFont = False
  244. .Shadow = False
  245. .Underline = xlUnderlineStyleNone
  246. .ColorIndex = 1
  247. .TintAndShade = 0
  248. .ThemeFont = xlThemeFontNone
  249. End With
  250. Range("O9:P35").Select
  251. Selection.Copy
  252. Range("E9:F35").Select
  253. ActiveSheet.Paste
  254. Application.CutCopyMode = False
  255.  
  256. End Sub

  

调用Excel宏批量处理文件的更多相关文章

  1. bat文件调用cmd命令批量提取文件夹中的文件名(批量修改文件扩展名)

    前言: 在平时的工作中,经常需要批量统计文件和数据,如果逐个统计的话太耗时,而且容易出错那么有没有什么快速的方法呢,这里给大家介绍一种简单高效的方法. 方法: 1.打开CMD命令: 按下 Ctrl+R ...

  2. Excel VBA批量修改文件夹下的文件名

    今天,有同事提出想批量修改文件名,规则比较简单,在第五位后加“-”即可, 上网没找到相关工具,就自己做了个excel,用宏代码修改. 代码如下: Private Sub CommandButton1_ ...

  3. C#调用Excel宏

    using System; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsFormsApplication1 { /// ...

  4. C# 调用 Excel 宏的方法

    调用方式是使用 Microsoft.Office.Interop.Excel.dll 组件来调用,该组件可以通过 Excel.exe 来生成,具体步骤如下: 第一步,进入 visual Studio ...

  5. C#调用Excel VBA宏

    近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有Excel中写VBA的,也有直接C#做的工具.有时需要在C#中执行Excel VBA宏,甚至有时还需要在执行了VBA宏之后,获取返回值再进 ...

  6. C#调用Excel VBA宏[转载]

    原文地址:https://www.cnblogs.com/heekui/archive/2008/03/30/1129355.html 近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有E ...

  7. 功能区按钮调用Excel、PowerPoint、Word中的VBA宏:RunMacro

    功能区按钮调用Excel.PowerPoint.Word中的VBA宏:RunMacro 众所周知,Excel.PPT.Word文档或加载宏文件中可以写很多过程和函数,调试的过程中当然可以按F8或F5直 ...

  8. 使用vbs调用excel中的宏

    使用vbs打开excel文件,并且传递参数调用excel中的macro,自动化完成excel文件的制作. Set oExcel = createobject("Excel.Applicati ...

  9. Excel技巧--批量生成指定名称的文件夹

    当我要按excel表当中的名字来批量生成文件夹时,手动一个个制作很麻烦(特别是成百上千个时).于是我们可以这么做: 1.在名字右侧建立公式:"MD "&A2. 2.将公式拖 ...

随机推荐

  1. 创建Oracle表空间

    *分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\ ...

  2. Url的拦截问题

    如果 在中没有, 就会去找欢迎页,先找/index,jsp,在webapp里并没有,就继续往下找/index.html, 是图中的拦截形式,就会被servlet拦截,对应于controller中的请求 ...

  3. R语言输出高质量图片

    Rstudio画图之后保存的 图片格式如下 上面的几种格式可以直接插入word文档中,但是图片质量很低,锯齿感很明显.若生成PDF,为矢量图(不懂),但是不可以插入word文档中. 最简便的方法就是对 ...

  4. Linux系统下常用命令

    yum [options] [command] [package ...] options:可选,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes"),-q(不显示安装 ...

  5. Autowried注解和Resource注解的区别

    目录 1.概述 2.Autowried 3.Resource 4.总结 1.概述 在使用Spring框架的过程中, 依赖注入是必须的, 大多时候会使用Autowried注解来进行依赖注入, 但是也可以 ...

  6. 关于在centos6 + grub的旧版本中,如何关闭CPU throttling

    由于个人需求,要编译安装ATLAS库,其中就有关闭CPU throttling的步骤, 最常规简单的方法是修改grub /etc/default/grub/ 之后再接一些简单的步骤 + 重启就完成了. ...

  7. Spring的诞生

    前言:不先学习常见的设计模式直接看Spring.MyBatis等源码,简直就是一个找虐的过程!不掌握Servlet原理.基本的Tomcat容器技术上来就看Spring MVC源码同样也是一个打击自信心 ...

  8. 搭建redis-sentinel(哨兵机制)集群

    redis怎么才能做到高可用 对于redis主从架构,slave可以对应多个本身可以保障高可用,但是对于一个master节点,如果宕机,整个缓存系统就无法进行写的操作,显然整个系统会无法做到高可用 s ...

  9. centos7下部署mariadb+galera数据库高可用集群

    [root@node1 ~]# cat /etc/yum.repos.d/mariadb.repo # MariaDB 10.1 CentOS repository list - created 20 ...

  10. C++指针和字符串

    ]="rose'; cout<<flowers<<endl; 数组名是第一个元素的地址,