Is there a limit on an Excel worksheet's name length?

后端 未结 5 955
天命终不由人
天命终不由人 2020-12-03 06:26

When I try to set a longish worksheet name using ruby and win32ole with the following code:

require \"win32ole\"
excel = WIN32OLE.new(\'Excel.Application\')
         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-03 06:49

    I use the following vba code where filename is a string containing the filename I want, and Function RemoveSpecialCharactersAndTruncate is defined below:

    worksheet1.Name = RemoveSpecialCharactersAndTruncate(filename)
    
    'Function to remove special characters from file before saving
    
    Private Function RemoveSpecialCharactersAndTruncate$(ByVal FormattedString$)
        Dim IllegalCharacterSet$
        Dim i As Integer
    'Set of illegal characters
        IllegalCharacterSet$ = "*." & Chr(34) & "//\[]:;|=,"
        'Iterate through illegal characters and replace any instances
        For i = 1 To Len(IllegalCharacterSet) - 1
            FormattedString$ = Replace(FormattedString$, Mid(IllegalCharacterSet, i, 1), "")
        Next
        'Return the value capped at 31 characters (Excel limit)
        RemoveSpecialCharactersAndTruncate$ = Left(FormattedString$, _
                               Application.WorksheetFunction.Min(Len(FormattedString), 31))
    End Function
    

提交回复
热议问题