調用Excel巨集批量處理文件

来源:http://www.cnblogs.com/jordonin/archive/2016/09/13/5867155.html
-Advertisement-
Play Games

...


 

'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

  

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 系統 # uname -a # 查看內核/操作系統/CPU信息# head -n 1 /etc/issue # 查看操作系統版本# cat /proc/cpuinfo # 查看CPU信息# hostname # 查看電腦名# lspci -tv # 列出所有PCI設備# lsusb -tv # 列 ...
  • 1、ifconfig命令找不到 解決方法:安裝net-tools.x86_64工具包 yum install net-tools.x86_64 2、iptables無法使用 解決方法:yum install iptables-services vi /etc/sysconfig/iptables # ...
  • 一、安裝GCC編譯環境,如果有則不需要 1)安裝mpc庫 2)安裝gmp庫 3)安裝mpfr庫 4) 安裝GCC 以上GCC的安裝不綴述,可參考各種大神步驟; 二、安裝pcre庫 https://sourceforge.net/projects/pcre/files/pcre/ 下載後解壓併在解壓文 ...
  • 這種情況一般是由於系統防火牆設置問題導致的,這次遇到的系統是centos 7.2,防火牆由iptables改成了firewall,因此停止防火牆的命令應該是: 禁止防火牆啟動的命令應該是: 暫時只寫這麼多,關於防火牆的配置等以後有時間了再說. ...
  • 繼續解決mplayer安裝不上的問題: 多次嘗試後,把源換回官方然後 update&upgrade後安裝 問題解決 時區問題解決: 裡面的第五項 Internationalisation Options --> Change Timezone --> Asia --> Chongqing(找了半天沒 ...
  • 之前裝的是live版 就是沒有桌面的版本,想看能hdmi看電影,於是找了教程安裝omxplayer 用 命令 通過hdmi播放電影 具體安裝過程發在貼吧里了:http://tieba.baidu.com/p/4766986525?see_lz=1 但是依然不能掛字幕.... 無奈今天重裝rasbia ...
  • 近期項目查詢資料庫太慢,持久層也沒有開啟二級緩存,現希望採用Redis作為緩存。為了不改寫原來代碼,在此採用AOP+Redis實現。 目前由於項目需要,只需要做查詢部分: 數據查詢時每次都需要從資料庫查詢數據,資料庫壓力很大,查詢速度慢,因此設置緩存層,查詢數據時先從redis中查詢,如果查詢不到, ...
  • 在前面的幾篇關於Free編程的討論示範中我們均使用了基礎類型的運算結果。但在實際應用中因為需要考慮運算中出現異常的情況,常常會需要到更高階複雜的運算結果類型如Option、Xor等。因為Monad無法實現組合(monad do not compose),我們如何在for-comprehension中 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...