VBN use to modify multiple batch of Excel spreadsheet format

Sub BatchChangeFormatOfExcel()

    Dim time1 As Date
    Dim time2 As Date
    time1 = Timer 'timing
    
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xExcelFile As String

    Application.DisplayAlerts = False
    Application.StatusBar = True
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xExcelFile = Dir(xSPath & "*.xlsx")
    Do While xExcelFile <> ""
        Application.StatusBar = "Changing: " & xExcelFile
        
        'Open Table
        Dim wB As Workbook
        Set wB = Workbooks.Open(Filename:=xSPath & xExcelFile)
        
        Dim myRange As Range
        Dim myFont As Font
    
        'Get the form and the rows to edit
        Set myRange = wB.Worksheets("Sheet1").Range("A1:E1")
        myRange.Merge 'combined
        Set myFont = myRange.Font 'Gets the font
        With myFont 'modify the font
            .Name = "Chinese New Wei"
            .Size = 20
            .Bold = True
        End With
                                    'Centered
        myRange.HorizontalAlignment = xlCenter
                                    'Format Column
        wB.Worksheets("Sheet1").Range("A:D").EntireColumn.NumberFormatLocal = "0000.000"
        
        'Save and Close
        wB.Save
        wB.Close
        
        'Get the next xlsx file
        xExcelFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
    
    'Processed tips, and total time
    time2 = Hours
    MsgBox "Finished!" & "  Cost Time: " & Format(time2 - time1, "Fixed") & " s."
    
End Sub

  

Guess you like

Origin www.cnblogs.com/xingzhensun/p/11961595.html