【问题标题】:Delete all text to right of variable character position based on variable string length根据可变字符串长度删除可变字符位置右侧的所有文本
【发布时间】:2018-08-15 00:05:15
【问题描述】:

问题:我有一列,其中每个单元格都有不同的长度和不同的句子结构。如果任何单元格的长度超过 1000 个字符,则在字符串中第 998 个字符的右侧找到第一次出现的 [句点]、[逗号]、[分号]、[冒号]并将该字符替换为 [3 个句点](应用程序替代省略号)。最后在 3 个句点之后截断所有剩余的文本。

例子-

当前数据: [[900 个以前的字符]]。 Visual Basic for Applications 支持构建用户定义的函数 (UDF)、自动化流程以及通过动态链接库 (DLL) 访问 Windows API 和其他低级功能。

预期输出: [[900 个以前的字符]]。 Visual Basic for Applications 支持构建用户定义的函数 (UDF)...

在“当前数据”中,长度 = 1098 个字符。第 998 个字符是 "procesS" 中的第二个 's'。右边第一次出现的所需标点符号是(UDF)之后的[逗号]。替换为 [3 个句点] 并删除字符串的其余部分。

目前这就是我所拥有的。我还没有想出如何包含各种条件来查找或如何在 3 个句点之后截断文本。此外,可能有一种更清洁的方式来完成所有这些操作。

For i = 2 To LR


If Len(Cells(i, 2).Value) > 1000 Then

    Cells(i, 2).Value = Left(Cells(i, 2), 998)
    Cells(i, 2).Value = StrReverse(Replace(StrReverse(Cells(i, 2).Value), StrReverse("."), StrReverse("..."), Count:=1))


End If
Next i

希望我已经提供了大量关于我正在尝试的信息。

【问题讨论】:

    标签: string excel replace string-length vba


    【解决方案1】:

    试试这个演示(可能对你有帮助)

    Sub Demo()
    Dim s As String
    Dim p As Integer
    
    s = "ab:cde,fghij Hello world, thanks a lot , for everything and "
    
    p = InStr(10, s, ",")
    Debug.Print p
    
    s = Mid(s, 1, p - 1) & "..."
    Debug.Print s
    End Sub
    

    另一个演示,如果您将处理更多选项(逗号/句点/分号)

    Sub Demo2()
    Dim a As Variant
    Dim s As String
    Dim p As Integer
    Dim p1 As Integer
    Dim p2 As Integer
    Dim p3 As Integer
    
    s = "ab:cde,fghij Hello ; world, thanks. a lot , for everything and "
    
    p1 = InStr(10, s, ",")
    p2 = InStr(10, s, ";")
    p3 = InStr(10, s, ".")
    
    a = Array(p1, p2, p3)
    p = Evaluate("MIN(IF({" & Join(a, ";") & "}>0,{" & Join(a, ";") & "}))")
    Debug.Print p
    
    s = Mid(s, 1, p - 1) & "..."
    Debug.Print s
    End Sub
    

    【讨论】:

    • 这些似乎是在最左边的 999 个字符中找到第一次出现,而不是最后一次出现。也许我读错了帖子。
    【解决方案2】:

    最左边 1000 个字符中最后一个符合条件的标点的位置可以用 InStrRev 定位。

    dim str as string, p as long
    for i=2 to lr
        str = cells(i, "B").value2
        if len(str) > 1000 then
            p = application.max(instrrev(str, chr(44), 998), _
                                instrrev(str, chr(46), 998), _
                                instrrev(str, chr(58), 998), _
                                instrrev(str, chr(59), 998))
            cells(i, "B") = left(str, p-1) & string(3, chr(46))
        end if
    next i
    

    【讨论】:

    • 这是我一开始的想法:)
    【解决方案3】:

    试试这个检查任何标点符号. , ; : 在 998 个字符之后的第一次出现。

    Dim teststring As String, firstcut As String, extension As String
    
    teststring = String$(1000, "a") & _
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
        "In malesuada non enim nec posuere. Praesent placerat nulla enim, " & _
        "at porta justo pharetra ac."
    
    If Len(teststring) > 999 Then
        firstcut = Left$(teststring, 998)
    
        extension = Right(teststring, Len(teststring) - 998)
        extension = Replace(Replace(Replace(extension, ",", "."), ";", "."), ":", ".")
        extension = Left$(extension, InStr(1, extension, ".") - 1) & "..."
    
        Debug.Print extension
    End If
    

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2017-11-03
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2018-11-25
      相关资源
      最近更新 更多