调用Excel宏批量处理文件
- '1.用户可以任意选择文件夹进行遍历
- '2.限定遍历时仅搜索EXCEL文件(你可以改变文件类型)
- '这个程序要先在“引用”下选择"microsoft scripting runtime"库文件
- Dim ArryFile() As String
- Dim nFile As Integer
- Sub Filelist()
- Dim fso As New FileSystemObject
- Dim fd As Folder
- Dim strFilePath As String
- Dim FolderSelect As FileDialog
- Set FolderSelect = Application.FileDialog(msoFileDialogFolderPicker)
- With FolderSelect
- If .Show = -1 Then
- strFilePath = .SelectedItems.Item(1) & "\"
- End If
- End With
- Set fd = fso.GetFolder(strFilePath)
- nFile = 0
- searchFile fd
- End Sub
- Private Function searchFile(ByVal fd As Folder)
- Dim fl As File
- Dim subfd As Folder
- Dim i As Integer
- On Error Resume Next
- i = fd.files.Count
- ReDim Preserve ArryFile(1 To nFile + i)
- For Each fl In fd.files
- If Right(fl.Name, 4) = "xlsx" Then '后缀是xls的用 If Right(fl.Name, 3) = "xls" Then
- nFile = nFile + 1
- ArryFile(nFile) = fl.Path
- End If
- Next
- If fd.SubFolders.Count = 0 Then Exit Function
- For Each subfd In fd.SubFolders
- searchFile subfd
- Next
- End Function
- //主函数,运行时调用该函数
- Sub ttt1()
- Dim xlname, myxl As Object, sh As Object
- Call Filelist
- 'Set myxl = CreateObject("Aplication.Excel")
- If nFile > 0 Then
- For Each xlname In ArryFile()
- If xlname <> "" Then
- //打开
- Workbooks.Open Filename:=xlname
- //调用Excel处理函数
- Call Macro3
- //保存,关闭
- ActiveWorkbook.Save
- ActiveWorkbook.Close
- End If
- Next
- End If
- Set myxl = Nothing
- End Sub
- //Excel处理函数,该段替换成自己的处理过程
- Sub Macro3()
- '
- ' Macro3 Macro
- '
- ' 快捷键: Ctrl+Shift+C
- '
- Range("V3:X3").Select
- ActiveCell.FormulaR1C1 = "/"
- With ActiveCell.Characters(Start:=1, Length:=1).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- Range("B5:J5").Select
- ActiveCell.FormulaR1C1 = "R种植业 □林业 □畜牧业 □渔业 □其他 "
- With ActiveCell.Characters(Start:=1, Length:=1).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=2, Length:=3).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=5, Length:=2).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=7, Length:=3).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=10, Length:=2).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=12, Length:=4).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=16, Length:=4).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=20, Length:=3).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=23, Length:=4).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=27, Length:=3).Font
- .Name = "宋体"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- With ActiveCell.Characters(Start:=30, Length:=1).Font
- .Name = "Wingdings 2"
- .FontStyle = "常规"
- .Size = 10
- .Strikethrough = False
- .Superscript = False
- .Subscript = False
- .OutlineFont = False
- .Shadow = False
- .Underline = xlUnderlineStyleNone
- .ColorIndex = 1
- .TintAndShade = 0
- .ThemeFont = xlThemeFontNone
- End With
- Range("O9:P35").Select
- Selection.Copy
- Range("E9:F35").Select
- ActiveSheet.Paste
- Application.CutCopyMode = False
- End Sub
调用Excel宏批量处理文件的更多相关文章
- bat文件调用cmd命令批量提取文件夹中的文件名(批量修改文件扩展名)
前言: 在平时的工作中,经常需要批量统计文件和数据,如果逐个统计的话太耗时,而且容易出错那么有没有什么快速的方法呢,这里给大家介绍一种简单高效的方法. 方法: 1.打开CMD命令: 按下 Ctrl+R ...
- Excel VBA批量修改文件夹下的文件名
今天,有同事提出想批量修改文件名,规则比较简单,在第五位后加“-”即可, 上网没找到相关工具,就自己做了个excel,用宏代码修改. 代码如下: Private Sub CommandButton1_ ...
- C#调用Excel宏
using System; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsFormsApplication1 { /// ...
- C# 调用 Excel 宏的方法
调用方式是使用 Microsoft.Office.Interop.Excel.dll 组件来调用,该组件可以通过 Excel.exe 来生成,具体步骤如下: 第一步,进入 visual Studio ...
- C#调用Excel VBA宏
近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有Excel中写VBA的,也有直接C#做的工具.有时需要在C#中执行Excel VBA宏,甚至有时还需要在执行了VBA宏之后,获取返回值再进 ...
- C#调用Excel VBA宏[转载]
原文地址:https://www.cnblogs.com/heekui/archive/2008/03/30/1129355.html 近日的一系列工作是做网站的营运维护,因此做了大量的支持工具.有E ...
- 功能区按钮调用Excel、PowerPoint、Word中的VBA宏:RunMacro
功能区按钮调用Excel.PowerPoint.Word中的VBA宏:RunMacro 众所周知,Excel.PPT.Word文档或加载宏文件中可以写很多过程和函数,调试的过程中当然可以按F8或F5直 ...
- 使用vbs调用excel中的宏
使用vbs打开excel文件,并且传递参数调用excel中的macro,自动化完成excel文件的制作. Set oExcel = createobject("Excel.Applicati ...
- Excel技巧--批量生成指定名称的文件夹
当我要按excel表当中的名字来批量生成文件夹时,手动一个个制作很麻烦(特别是成百上千个时).于是我们可以这么做: 1.在名字右侧建立公式:"MD "&A2. 2.将公式拖 ...
随机推荐
- 创建Oracle表空间
*分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\ ...
- Url的拦截问题
如果 在中没有, 就会去找欢迎页,先找/index,jsp,在webapp里并没有,就继续往下找/index.html, 是图中的拦截形式,就会被servlet拦截,对应于controller中的请求 ...
- R语言输出高质量图片
Rstudio画图之后保存的 图片格式如下 上面的几种格式可以直接插入word文档中,但是图片质量很低,锯齿感很明显.若生成PDF,为矢量图(不懂),但是不可以插入word文档中. 最简便的方法就是对 ...
- Linux系统下常用命令
yum [options] [command] [package ...] options:可选,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes"),-q(不显示安装 ...
- Autowried注解和Resource注解的区别
目录 1.概述 2.Autowried 3.Resource 4.总结 1.概述 在使用Spring框架的过程中, 依赖注入是必须的, 大多时候会使用Autowried注解来进行依赖注入, 但是也可以 ...
- 关于在centos6 + grub的旧版本中,如何关闭CPU throttling
由于个人需求,要编译安装ATLAS库,其中就有关闭CPU throttling的步骤, 最常规简单的方法是修改grub /etc/default/grub/ 之后再接一些简单的步骤 + 重启就完成了. ...
- Spring的诞生
前言:不先学习常见的设计模式直接看Spring.MyBatis等源码,简直就是一个找虐的过程!不掌握Servlet原理.基本的Tomcat容器技术上来就看Spring MVC源码同样也是一个打击自信心 ...
- 搭建redis-sentinel(哨兵机制)集群
redis怎么才能做到高可用 对于redis主从架构,slave可以对应多个本身可以保障高可用,但是对于一个master节点,如果宕机,整个缓存系统就无法进行写的操作,显然整个系统会无法做到高可用 s ...
- centos7下部署mariadb+galera数据库高可用集群
[root@node1 ~]# cat /etc/yum.repos.d/mariadb.repo # MariaDB 10.1 CentOS repository list - created 20 ...
- C++指针和字符串
]="rose'; cout<<flowers<<endl; 数组名是第一个元素的地址,