EXCEL——VBA對文件夾下所有表格的特定單元格賦值

image

不知道大家有沒有遇到過這種情況,一個文件夾下有很多Excel文件,每個文件里面一個或多個表都有一個同樣的單元格,比如自己的名字啊、生日啊之類的。這個時候如果要改動這個單元格的內(nèi)容我們需要一個個改動,就非常麻煩。這個時候我們可以通過VBA的方式來解決這個問題。。

image.png
  • 如上圖,我們這個文件夾下有兩個表格,每個表格中都有幾個sheet,這幾個sheet中有幾乎都有一個單元格內(nèi)容如下:


    image.png

我們現(xiàn)在要把這個文件夾下所有EXCEL表里面所有sheet中的這個李明后面的內(nèi)容改成17歲,應該怎么改呢?

Sub 遍歷替換()
    Dim FindRng As Range
    Dim temp
    lookingname = "李明"
    Price = 22
    Application.ScreenUpdating = False
    Dim Filename, wb As Workbook, Sht As Worksheet
    Filename = Dir(ThisWorkbook.Path & "\*.xlsm")
    MsgBox ThisWorkbook.Path
    MsgBox Filename
    Do While Filename <> ""
        If Filename <> ThisWorkbook.Name Then
            fn = ThisWorkbook.Path & "\" & Filename
            Set wb = Workbooks.Open(fn)
            'Set Sht = wb.Worksheets(1)
                For Each temp In wb.Worksheets
                    Set FindRng = temp.UsedRange.Find(lookingname, lookat:=xlWhole)
                    If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
                Next
                'Set FindRng = Sht.UsedRange.Find(lookingname, lookat:=xlWhole)
                'If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
            wb.Close True
        End If
        Filename = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

利用這段代碼,可以將同一文件夾下的其他表格(非當前執(zhí)行代碼的表格)中所有sheet中的“李明”后面的單元格內(nèi)容替換。極大的提高了效率,尤其在文件很多的情況下。

  • 下面我們來對代碼進行一下講解
Filename = Dir(ThisWorkbook.Path & "\*.xlsm")

用Dir來對當前表格下的所有xlsm進行遍歷,下面這個循環(huán)便是對每個表格進行操作

Do While Filename <> ""
        If Filename <> ThisWorkbook.Name Then
            fn = ThisWorkbook.Path & "\" & Filename
            Set wb = Workbooks.Open(fn)
            'Set Sht = wb.Worksheets(1)
                For Each temp In wb.Worksheets
                    Set FindRng = temp.UsedRange.Find(lookingname, lookat:=xlWhole)
                    If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
                Next
                'Set FindRng = Sht.UsedRange.Find(lookingname, lookat:=xlWhole)
                'If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
            wb.Close True
        End If
        Filename = Dir
 Loop

其中

fn = ThisWorkbook.Path & "\" & Filename
            Set wb = Workbooks.Open(fn)
            'Set Sht = wb.Worksheets(1)
                For Each temp In wb.Worksheets
                    Set FindRng = temp.UsedRange.Find(lookingname, lookat:=xlWhole)
                    If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
                Next
                'Set FindRng = Sht.UsedRange.Find(lookingname, lookat:=xlWhole)
                'If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price
            wb.Close True

這一段代碼是將文件夾下每個文件都打開然后對其中的sheet進行遍歷,并通過

 Set FindRng = temp.UsedRange.Find(lookingname, lookat:=xlWhole)
                    If Not FindRng Is Nothing Then FindRng.Offset(, 1) = Price

這兩句代碼來查找特定單元格并賦值。最后一個接一個打開又關(guān)閉workbook來達到替換所有單元格的效果。。。
最后,我們就可以看到所有的“李明”后面的單元格,都改變成了代碼中指定的內(nèi)容:


image.png

其他sheet也是已經(jīng)更改,就不一一截圖了~~

就是這樣,希望能幫到大家,謝謝~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自鏈接 目錄 1.認識NPOI 2.使用NPOI生成xls文件 2.1創(chuàng)建基本內(nèi)容 2.1.1創(chuàng)建Workboo...
    腿毛褲閱讀 11,165評論 1 3
  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長城ol閱讀 8,755評論 2 25
  • VBA訂制工具欄 http://club.excelhome.net/thread-1047254-1-1.htm...
    大海一滴寫字的地方閱讀 2,357評論 0 0
  • 1.1 VBA是什么 直到90年代早期,使應用程序自動化還是充滿挑戰(zhàn)性的領(lǐng)域.對每個需要自動化的應用程序,人們不得...
    浮浮塵塵閱讀 22,153評論 6 49
  • 本例為設(shè)置密碼窗口 (1) If Application.InputBox(“請輸入密碼:”) = 1234 Th...
    浮浮塵塵閱讀 14,802評論 1 20

友情鏈接更多精彩內(nèi)容