EXCEL VBA字符串长度超过3行,基于长度,空格," /"或" _"字符

时间:2018-04-19 05:51:01

标签: excel vba

根据Zac给我的代码

String length over 3 lines based on length

我现在需要对其进行更新,以包含文本/单词不能用空格分隔的可能性,还包括字符" /"或" _"。我没有成功,有一个好小提琴。 我在函数中添加了if语句,如: -

If InStr(55, sText, "_") > 0 Then
        sGreater55 = Mid(sText, InStr(55, sText, "_"))

没有成功: - (

Function SetString(ByVal sText As String) As String
    Dim sGreater55$, sGreater19$

'如果文本大于55个字符,首先让我们在55个字符后捕获字符串(取决于SPACE字符的位置)

If Len(sText) > 55 Then
    If InStr(55, sText, " ") > 0 Then
        sGreater55 = Mid(sText, InStr(55, sText, " "))
    Else
        sGreater55 = Mid(sText, InStrRev(sText, " ", 55))
    End If
End If

'如果text大于19个字符,则让我们在19个字符后构建字符串(取决于SPACE字符所在的位置)

If Len(sText) > 19 Then
    If InStr(19, sText, " ") > 0 Then
        sGreater19 = Mid(sText, InStr(19, sText, " "))
    Else
        sGreater19 = Mid(sText, InStrRev(sText, " ", 19))
    End If
    sGreater19 = Left(sGreater19, Len(sGreater19) - Len(sGreater55))
End If

'现在让我们构建完整的字符串

SetString = Left(sText, Len(sText) - (Len(sGreater19) + Len(sGreater55))) & vbLf & sGreater19 & vbLf & sGreater55

End Function

2 个答案:

答案 0 :(得分:1)

首先假设:

  • 提供给UDF的文本将具有分隔文本的3个“分隔符”之一(即文本被空格或“_”或“/”分隔)。提供的文本不是由“分隔符”组合分隔的(即没有像blah blah blah\blah\blah这样的文本)
  • 如果提供的文本中不存在3个“分隔符”,则返回空字符串

根据以上假设,试试这个:

Function SetString(ByVal sText As String) As String
    Dim sGreater55$, sGreater19$
    Dim sChrToCheck As String: sChrToCheck = ""

    ' First lets decide which character we need to use to seperate string
    If InStr(1, sText, " ") > 0 Then
        sChrToCheck = " "
    ElseIf InStr(1, sText, "_") > 0 Then
        sChrToCheck = "_"
    ElseIf InStr(1, sText, "/") > 0 Then
        sChrToCheck = "/"
    Else
        ' Non of the expected text dividers found in the specified string to return an empty string
        Exit Function
    End If

    ' If text is greater than 55 characters, first lets capture the string after 55 characters (depending on where the SPACE character is)
    If Len(sText) > 55 Then
        If InStr(55, sText, sChrToCheck) > 0 Then
            sGreater55 = Mid(sText, InStr(55, sText, sChrToCheck))
        Else
            sGreater55 = Mid(sText, InStrRev(sText, sChrToCheck, 55))
        End If
    End If

    ' If text is greater than 19 characters, lets build the string after 19 characters (depending on where the SPACE character is)
    If Len(sText) > 19 Then
        If InStr(19, sText, " ") > 0 Then
            sGreater19 = Mid(sText, InStr(19, sText, sChrToCheck))
        Else
            sGreater19 = Mid(sText, InStrRev(sText, sChrToCheck, 19))
        End If
        sGreater19 = Left(sGreater19, Len(sGreater19) - Len(sGreater55))
    End If

    ' Now lets build the complete string
    SetString = Left(sText, Len(sText) - (Len(sGreater19) + Len(sGreater55))) & vbLf & sGreater19 & vbLf & sGreater55

End Function

答案 1 :(得分:1)

假设可能混合使用 split 字符(例如 Group Setting Runtime Memory SomeOtherColumns ... A X 102 105 ... A Y 100 104 ... B X 8 52 ... B Y 13 60 ... C X 5 6 ... C Y 6 7 ... :创建输入字符串的副本,请替换所有可能的 split 字符空白并轻微修改功能:

blah_blah_blah blah_blah/blahblah