How to use VBA to change the contents of the batch file Excel

Reviews (5): Using Excel VBA batch change file content
problem: Because all Excel file format accepted is the same, but there is a content of a cell is required, the organizers to change as follows:
Here Insert Picture Description
the winning level, too to change by the organizers, but if so, then opened one by one is very troublesome, (how to put all the files into individual files, first prize, second prize and so on, is not yet know, I was by one to exclude) after separating them, the direct use of VBA rapid change,

Change steps:

  1. First is to configure the environment, to avoid errors as follows:
    Here Insert Picture Description
  2. Then start the cycle of each file, as a file before
  3. Then open each file, change specific content:
    Here Insert Picture Description
    just have to remember to save the file, VBA file is not going to help you quit automatically saved
  4. Continuous cycle, then you can exit the program

Source as follows:

Sub 批量改名()
    Dim mypath As String, myname As String, awbname As String
    Dim wbcount As Integer

    '关闭excel的刷新
    Application.ScreenUpdating = False

    '禁止弹出对话框
    Application.DisplayAlerts = False
    
    '得到本文件的相对地址
    mypath = ActiveWorkbook.Path
    
    '当前工作的excel的文件名
    awbname = ActiveWorkbook.Name

    '任意打开文件夹下的某一个文件
    wbcount = 0
    myname = Dir(mypath & "\" & "*.xlsx")

    '如果当前的文件名为空的字符串("")表示已经没有更多的文件了跳出循环
    Do While myname <> ""
        If myname <> awbname Then
            '打开当前的文件夹
            Set wb = Workbooks.Open(mypath & "\" & myname)

            wbcount = wbcount + 1
            
            Range("I" & 5).Value = "三等奖"
			
			Range("I" & 5).Font.ColorIndex = 0
			
			'找了很久才找到了这个错误,因为如果不保存的话退出是不会有改变的
            wb.save
			
            '关闭文件
            wb.Close False
            
        End If
        '随机打开本文件夹的另一个文件
        myname = Dir
    Loop

'结束程序并且恢复之前的操作
MsgBox "一共更改了 " & wbcount & " 个文件"
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
Published 41 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42224330/article/details/100559421