调用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

  

时间: 2024-08-09 17:40:33

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

vb调用excel方法详解及操作相关操作命令大全

如果你要在VB中要想调用Excel,需要打开VB编程环境"工程"菜单中的"引用"项目,并选取项目中的"Microsoft Excel 11.0 object library"项.由于你的Excel版本不同,所以这个选项的版本号也是不同的. 因为EXCEL是以层次结构组织对象的,其对象模型中含有许多不同的对象元素.    第一层:Application对象,即Excel本身:    第二层:workbooks对象集,指Excel的工作簿文件: 第三

Ruby调用Excel相关的函数

require 'win32ole'myexcel = WIN32OLE.new("excel.application")#WIN32OLE.open 方法打开用例文件,用Call sub方法(如果有的话) 来调用Excel中的静态检查的宏myexcel.visible=truemywbk = myexcel.Workbooks.Add()mywst= mywbk.Worksheets(1)mywst.Range('A1:D1').value =['1','2','3','4']myw

【游戏开发】Excel表格批量转换成CSV的小工具

一.前言 在工作的过程中,我们有时可能会面临将Excel表格转换成CSV格式文件的需求.这尤其在游戏开发中体现的最为明显,策划的数据文档大多是一些Excel表格,且不说这些表格在游戏中读取的速度,但就论占用内存来说,同样的数据量Excel表格所占用的内存要远远大于CSV,因此将Excel转换成CSV势在必行.如果单单转换一个Excel表格还好,直接另存为就搞定的,但是如何将一个文件下的N个Execl表格转成CSV呢?今天马三就来和大家一起用Python撸一个Excel表格批量转换CSV的小工具—

zabbix调用api接口批量添加主机

zabbix调用api接口批量添加主机,这里用的是python 2.7(需安装xlrd模块处理表格). (1)到官网下载xlrd模块:https://pypi.python.org/pypi/xlrd (2)解压压缩包:tar xvf xlrd-1.0.0.tar.gz(3)进入到解压的文件夹运行python setup.py install命令安装 1.整理一个excel表格(test.xlsx),格式如下: 2.addhost.py脚本,添加主机名.显示名称.dns主机名.群组.模板字段(网

【游戏开发】Excel表格批量转换成lua的转表工具

一.简介 在上篇博客<[游戏开发]Excel表格批量转换成CSV的小工具> 中,我们介绍了如何将策划提供的Excel表格转换为轻便的CSV文件供开发人员使用.实际在Unity开发中,很多游戏都是使用Lua语言进行开发的.如果要用Lua直接读取CSV文件的话,又要写个对应的CSV解析类,不方便的同时还会影响一些加载速度,牺牲游戏性能.因此我们可以直接将Excel表格转换为lua文件,这样就可以高效.方便地在Lua中使用策划配置的数据了.在本篇博客中,马三将会和大家一起,用C#语言实现一个Exce

C#异步批量下载文件

C#异步批量下载文件 实现原理:采用WebClient进行批量下载任务,简单的模拟迅雷下载效果! 废话不多说,先看掩饰效果: 具体实现步骤如下: 1.新建项目:WinBatchDownload 2.先建一个Windows窗体:FrmBatchDownload,加载事件FrmBatchDownload_Load 3.放置一个Button按钮:btnStartDownLoad,单机事件btnStartDownLoad_Click 4.放置一个DataGridView:dgvDownLoad. 5.具

批量复制文件无密码输入

无密码输入批量复制文件 作用:批量复制:复制单个文件或者压缩包到多台服务器,无密码输入<我用着挺方便的> 名称:scp_file.py 环境:需要有Python,需要paramiko <在管理机上安装,一劳永逸> 执行方式: ./scp_file.py iplist src_file dst_file 说明: iplist:ip列表 src_file:绝对路径下的文件 例如:/tmp/test.txt dst_file:绝对路径下的文件 例如:/data/log1.txt 备注:复

c# 调用EXCEL在VS上能正常运行,部署在IIS上不能实现,在VS中运行页面和发布之后在IIS中运行的区别

发现一篇文章,很好,解决了这个问题:感谢原博主!特此做个笔记. 地址:http://www.cnblogs.com/zhongxinWang/p/3275154.html 发布在IIS上的Web程序,调用服务器的COM组件 场景大致是这样的,在工厂中分布着许多的PDA点,这些PDA点都要进行实时的扫描--打印操作.实现方法是采用网络打印机,然后服务器安装驱动,管理着所有的打印机.然后服务器,发布一个WebService,给每个PDA上的程序进行调用,PDA在要进行打印的时候,就掉用这个服务,传递

iOS APP中第三方APP调用自己的APP,打开文件

根据需求需要在项目中要打开word.pdf.excel等文件,在info.plist文件中添加 <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string></string> <key>LSItemContentTypes</key> <array> <strin