我正在尝试创建一个VBA脚本,用于更改保存在一个文件夹中的多个工作簿中的字体。但是,它不起作用。请看一下代码
Sub changefont()
Dim wb As Workbook, sh As Worksheet, fpath As String, fname As String
fpath = "D:\reports"
If Right(fpath, 1) <> "\" Then fpath = fpath & "\"
fname = Dir(fpath & ".xls")
Do
On Error Resume Next
Set wb = Workbooks.Open(fname)
Set sh = wb.Sheets("REPORT")
On Error GoTo 0
If Not sh Is Nothing Then
With sh.Range(Cells(10, 1), Cells(90, 11))
.Font.Size = "18"
.Font = "Arial"
End With
End If
wb.Close True
fname = Dir
Loop While fname <> ""
End Sub
注意:我的Sheet1在所有工作簿中都被命名为REPORT
答案 0 :(得分:0)
试试这个(未经测试)。我已在相关部分添加了评论。如果您收到错误或有任何问题,请告诉我。
Sub changefont()
Dim wb As Workbook, sh As Worksheet
Dim fpath As String, fname As String
fpath = "D:\reports"
If Right(fpath, 1) <> "\" Then fpath = fpath & "\"
fname = Dir(fpath & ".xls")
Do While fname <> ""
Set wb = Workbooks.Open(fname)
'~~> This is important
Set sh = Nothing
On Error Resume Next
Set sh = wb.Sheets("REPORT")
On Error GoTo 0
If Not sh Is Nothing Then
'~~> You need to fully qualify the cells object
With sh.Range(sh.Cells(10, 1), sh.Cells(90, 11))
'~> Font Size is not a string
.Font.Size = 18
'~~> Add .Name
.Font.Name = "Arial"
End With
wb.Close True
DoEvents
Else
wb.Close False
End If
fname = Dir
Loop
End Sub