
Tips.Net > ExcelTips Home > Macros > VBA Examples > Changing Fonts in Multiple Workbooks
Summary: If you need to change fonts used in a lot of different workbooks, the task can be daunting—if you need to do it manually. This tip looks at a macro you can use to make the task more automatic. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
Hamish is facing a daunting task: He needs to change the default fonts used in a large number of Excel workbooks. He has over 100 workbooks, and the fonts used in those workbooks need to be changed to a new font specified by corporate mandate. (You know how corporate mandates can be!)
The manual way to approach this task is to load each workbook, go through each worksheet, select the cells, and change the fonts in those cells. To make Hamish's task even more complex, he needs to change multiple fonts in each workbook. In other words, given fonts A, B, C, and D, Hamish needs to change font A to C and font B to D.
The best way to approach this problem is through the use of a macro. There is so much loading, searching, and changing that is necessary that it only makes sense to relegate the work to a macro. The following macro should do the job:
Sub ChangeFontNames()
Dim vNamesFind
Dim vNamesReplace
Dim sFileName As String
Dim Wkb As Workbook
Dim Wks As Worksheet
Dim rCell As Range
Dim x As Integer
Dim iFonts As Integer
Dim sPath As String
'Change these lines as appropriate
'These are the fontnames to find
vNamesFind = Array("Arial", "Allegro BT")
'These are the fontnames to replace
vNamesReplace = Array("Wingdings", "Times New Roman")
'This is the folder to look for xls files
sPath = "C:\foldername\"
Application.ScreenUpdating = False
iFonts = UBound(vNamesFind)
If iFonts <> UBound(vNamesReplace) Then
MsgBox "Find and Replace Arrays must be the same size"
Exit Sub
End If
sFileName = Dir(sPath & "*.xls")
Do While sFileName <> ""
Set Wkb = Workbooks.Open(sPath & sFileName)
For Each Wks In Wkb.Worksheets
For Each rCell In Wks.UsedRange
For x = 0 To iFonts
With rCell.Font
If .Name = vNamesFind(x) Then _
.Name = vNamesReplace(x)
End With
Next
Next
Next
Wkb.Close(True)
sFileName = Dir
Loop
Application.ScreenUpdating = True
Set rCell = Nothing
Set Wks = Nothing
Set Wkb = Nothing
End Sub
To use the macro with your own workbooks, there are a couple of things you need to do. First, make sure that all the workbooks you want to change are stored in a single folder and that you know the name of the folder. Then, within the macro, change the variables defined near the beginning of the macro. Change the elements of the vNamesFind and vNamesReplace arrays to match the names of the fonts you want to respectively find and replace. You should then change the sPath variable so it contains the full path to the folder containing your workbooks. (Don't forget a trailing backslash on the path.)
When you run the macro, it loads each workbook in the folder, in turn. Then, it goes through each worksheet in each workbook, and examines every cell. If the cell has one of the fonts to be found, then it is replaced with the respective replacement font. When the macro is done with the workbook, it is saved and the next workbook is processed.
Those interested in avoiding this type of problem on new worksheets should explore how to use styles in Excel. You can define any number of styles and use them throughout a workbook. If you later need to change the formatting for specific cells, all you need to do is change the underlying styles. (Styles have been covered in other issues of ExcelTips.)
Tip #2526 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Tame Your Data! ExcelTips: Filters and Filtering provides all the details necessary to let you manage large sets of data with confidence and ease. Its information-packed pages demonstrate how to use the two types of filters provided by Excel: AutoFilters and advanced filters.
Check out ExcelTips: Filters and Filtering today!
It doesn't matter if you are a beginner or expert, the ExcelTips archives are the fastest way to improve your productivity. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site