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

后端 未结 5 942
天命终不由人
天命终不由人 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
    
    0 讨论(0)
  • 2020-12-03 06:51

    I just tested a couple paths using Excel 2013 on on Windows 7. I found the overall pathname limit to be 213 and the basename length to be 186. At least the error dialog for exceeding basename length is clear: basename error

    And trying to move a not-too-long basename to a too-long-pathname is also very clear:

    The pathname error is deceptive, though. Quite unhelpful:

    This is a lazy Microsoft restriction. There's no good reason for these arbitrary length limits, but in the end, it’s a real bug in the error dialog.

    0 讨论(0)
  • 2020-12-03 06:55

    My solution was to use a short nickname (less than 31 characters) and then write the entire name in cell 0.

    0 讨论(0)
  • 2020-12-03 06:58

    Renaming a worksheet manually in Excel, you hit a limit of 31 chars, so I'd suggest that that's a hard limit.

    0 讨论(0)
  • 2020-12-03 07:05

    The file format would permit up to 255-character worksheet names, but if the Excel UI doesn't want you exceeding 31 characters, don't try to go beyond 31. App's full of weird undocumented limits and quirks, and feeding it files that are within spec but not within the range of things the testers would have tested usually causes REALLY strange behavior. (Personal favorite example: using the Excel 4.0 bytecode for an if() function, in a file with an Excel 97-style stringtable, disabled the toolbar button for bold in Excel 97.)

    0 讨论(0)
提交回复
热议问题