编写宏来在一个文件夹中的500多个工作簿中更改字体和大小

时间:2017-09-08 20:49:00

标签: excel vba excel-vba

我在一个文件夹中有500多个.xlsx个文件;它们具有相同的格式(图表和表格)。我需要做的是更改特定单元格的字体,大小和对齐方式。我从互联网上得到了一些宏,但它们似乎都没有用。我认为最接近的是:

Sub Font_Style()
    Dim wb As Workbook, sh As Worksheet, fPath As String, fName As String

    fPath = "C:\xxx\1234\"
    fName = Dir(fPath & "*.xlsx")

    Do
        Set wb = Workbooks.Open(fName)
        Set sh = wb.Sheets("Empty Form")

        With sh.Range("X17")
                    .Font.Size = 20
                    .Font.Name = "Arial"
                    .Font.Bold = True
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
        End With

        wb.Close True
        fName = Dir
    Loop While fName <> ""
End Sub

1 个答案:

答案 0 :(得分:1)

你可以用这个

Sub formatchange()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("your path")

    Application.ScreenUpdating=False 'for a fster code
    For Each objFile In objFolder.files
      Workbooks.Open (objFile)
      'put your formatting code here        
      ActiveWorkbook.Close savechanges:=True        
    Next

    'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

    Application.ScreenUpdating=True 'turn on updatin again
End Sub