Sub BatchChangeFormatOfExcel()
Dim time1 As Date
Dim time2 As Date
time1 = Timer ' 計時
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
' 打開表格
Dim wB As Workbook
Set wB = Workbooks.Open(Filename:=xSPath & xExcelFile)
Dim myRange As Range
Dim myFont As Font
' 獲取表格及要修改的列
Set myRange = wB.Worksheets("Sheet1").Range("A1:E1")
myRange.Merge ' 合並
Set myFont = myRange.Font ' 獲取字體
With myFont ' 修改字體
.Name = "華文新魏"
.Size = 20
.Bold = True
End With
' 居中顯示
myRange.HorizontalAlignment = xlCenter
' 設置列格式
wB.Worksheets("Sheet1").Range("A:D").EntireColumn.NumberFormatLocal = "0000.000"
' 保存並關閉
wB.Save
wB.Close
' 獲取下一個xlsx文件
xExcelFile = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
' 處理完畢提示,及總耗時
time2 = Timer
MsgBox "Finished!" & " Cost Time: " & Format(time2 - time1, "Fixed") & " s."
End Sub