I want to start by saying that I am a teacher and have very little experience coding. I only have a cursory understanding of how to manipulate cell values in pre-existing code. I created a spreadsheet for my school district to help teachers streamline the creation of student progress reports. On the .xlsm file I used the following code to auto-populate tab names based on the contents of cell "b1" in the same tab. I found this code on a forum. It worked well for a few years, but suddenly this year the code does not work. The code fails and displays the message box delineated in the vba code that the source cell contains illegal characters:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Set Target = Range("b1")
If Target = "" Then Exit Sub
On Error GoTo Badname
ActiveSheet.Name = Left(Target, 31)
Exit Sub
Badname:
MsgBox "Please revise the entry in b1." & Chr(13) _
& "It appears to contain one or more " & Chr(13) _
& "illegal characters." & Chr(13)
Range("b1").Activate
End Sub
Can this code be fixed? Does excel offer other means to auto-populate tab names from cell values? In case it matters, the cell that I am using as the source for the tab name contains the formula ='class list'!B5. It pulls the desired source text from a different tab and the goal is to have the text that is transferred via the formula to be the source of the tab's name. Is there a way to modify the VBA code to pull the value directly from the source tab? I could understand if it is the formula in "b1" that is causing the problem? But it is a mystery to me why, after working for several previous years, the problem is only now manifesting...
One other issue with this VBA code is that it required the user to interact with the cells in the tab before the VBA code would run and change the tab name. Could the code be made to change the tab name as soon as the source cell value changes? Though not strictly necessary this would be a big quality of life improvement.
Thank you so much for your consideration,
Eric