Jennifer Liu
asked on
How to resolve run time error "1004" name more than 31 charaters long
I am trying to generate some school reports and some of the school names are too long for the worksheet. How can I resolve this.
ASKER
I know some of the school names are too long so I need help in write a code to shorten the name of the schools if possible before I review the list of schools manually. Are you able to help?
I know some of the school names are too long so I need help in write a code to shorten the name of the schools if possible before I review the list of schools manuallysince the school name could longer that the characters allowed for the worksheet name, you may try to use a shorter code name or other unique identifier to represent the worksheet name instead.
You can simply truncate name:
If Len(sheetName)>31 Then sheetName = Left$(sheetName, 31)
But IMHO it's better create one more sheet (placing it in the first place) with abbreviations and fullnames columns and name worksheets with abbreviated names.
If Len(sheetName)>31 Then sheetName = Left$(sheetName, 31)
But IMHO it's better create one more sheet (placing it in the first place) with abbreviations and fullnames columns and name worksheets with abbreviated names.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
just a note,... you may also want to check the uniqueness of worksheet name in case you decided to truncate the worksheet name, else it may generate another runtime error (same worksheet name).
ASKER
Thank you I have resolved this issue by using left() and mid().
I would suggest that you reduce the names to comprehensible abbreviations. The advantages are:
Easy to pick up at first glance
Less chances of duplication
Have more tabs visible in the window
It would also be a good idea to have a separate sheet for sheet names and abbreviations for easy recollection in case you forget the abbreviation. This sheet can also be used to "Go to" any school sheet.
Easy to pick up at first glance
Less chances of duplication
Have more tabs visible in the window
It would also be a good idea to have a separate sheet for sheet names and abbreviations for easy recollection in case you forget the abbreviation. This sheet can also be used to "Go to" any school sheet.
Also, there is a Windows API that can be used to shorten names. It introduces an ellipsis into the shortened string.
>>there is a Windows API
Private Declare Function PathCompactPathEx Lib "shlwapi" Alias "PathCompactPathExA" _
(ByVal pszOut As String, _
ByVal pszSrc As String, _
ByVal cchMax As Long, _
ByVal dwFlags As Long) As Long
Private Sub Command1_Click()
Dim sIn As String, sOut As String, max_length As Long
sIn = "This is a very long string with length exceeding 31 char"
max_length = 31
sOut = String(max_length + 1, Chr(0)) '+1 for null char this API appends at the end
PathCompactPathEx sOut, sIn, max_length + 1, 0
sOut = Left$(sOut, max_length)
Debug.Print sOut
End Sub
VB internal string functions can do this too. This API can be usefull with real path - it truncates middle part of string leaving last path separator and file name.
ASKER
Thank you for your help. This code has helped my problem.
Pleased to help
Test if name contains too many characters
Open in new window
Incorporate it into your code something like this
Open in new window