Insert the following code into a module in the template workbook (the book
with the sheet titles you want). I put a measure in the code to handle
multiple sheets for the same name (ie. Mr Fred Bloggs and Mr Fred Harry
Bloggs). If you don't want it use the second code below.
Mike
Option Explicit
Sub UpdateSheetNames()
Dim This As Workbook, That As Workbook
Dim shthis As Worksheet, shthat As Worksheet, ws As Worksheet
Dim first As String, last As String
Dim i As Integer
Set This = ThisWorkbook
Set That = Workbooks.Open("P:\Excel Help\MatchSheets2.xls")
For Each shthis In This.Worksheets
i = 1
first = Left(shthis.Name, InStr(1, shthis.Name, " ") - 1)
last = Right(shthis.Name, Len(shthis.Name) - _
InStr(1, shthis.Name, " "))
For Each shthat In That.Worksheets
If InStr(1, shthat.Name, first) <> 0 And _
InStr(1, shthat.Name, last) <> 0 And _
InStr(1, shthat.Name, first) < InStr(1, shthat.Name, last) Then
On Error Resume Next
MsgBox (first & " " & last)
Set ws = That.Sheets(first & " " & last)
On Error GoTo 0
If ws Is Nothing Then
shthat.Name = shthis.Name
Else
shthat.Name = shthis.Name & i
i = i + 1
Set ws = Nothing
End If
End If
Next shthat
Next shthis
End Sub
'****** END OF CODE ******
'**** SECOND CODE ****
Option Explicit
Sub UpdateSheetNames()
Dim This As Workbook, That As Workbook
Dim shthis As Worksheet, shthat As Worksheet, ws As Worksheet
Dim first As String, last As String
Dim i As Integer
Set This = ThisWorkbook
Set That = Workbooks.Open("P:\Excel Help\MatchSheets2.xls")
For Each shthis In This.Worksheets
i = 1
first = Left(shthis.Name, InStr(1, shthis.Name, " ") - 1)
last = Right(shthis.Name, Len(shthis.Name) - _
InStr(1, shthis.Name, " "))
For Each shthat In That.Worksheets
If InStr(1, shthat.Name, first) <> 0 And _
InStr(1, shthat.Name, last) <> 0 And _
InStr(1, shthat.Name, first) < InStr(1, shthat.Name, last) Then
shthat.Name = shthis.Name
End If
End If
Next shthat
Next shthis
End Sub
'****** END OF CODE ******