裁剪字符串的最后N行以显示在userform文本框中

时间:2015-04-12 05:08:29

标签: vba excel-vba excel

我想在userform的文本框中显示一个textlog字符串。

代码可能如下所示:

Dim public textlog as string

sub button1_click()
' do some action
textlog = textlog & event_string & vbCrLf
'event_string might exceed more than 2 line 
textlog = textlog & "button1 action" & vbCrLf
userform1.textbox1.text = textlog 
end sub

sub button2_click()
' do some action
textlog = textlog & event_string & vbCrLf
'event_string might exceed more than 2 line 
textlog = textlog & "button2 action" & vbCrLf
userform1.textbox1.text = textlog 
end sub

但是,文本框应该只包含20行信息,而我的 我的课本内容将超过20行。

如何在textbox1中仅显示最新(最后)20行文本日志?

2 个答案:

答案 0 :(得分:0)

您可以使用此功能仅返回字符串的最后N行,然后在文本框中显示该行。

请注意,您必须指定换行符是什么。根据您的具体应用,可能是vbCrLfvbCrvbLf,甚至是其他分隔符。

Function GetLastLines(ByVal s As String, ByVal nLinesToDisplay As Long, _
    Optional ByVal lineBreakChar As String = vbCrLf)

    'Split the string into an array
    Dim splitString() As String
    splitString = Split(s, lineBreakChar)

    'How many lines are there?
    Dim nLines As Long
    nLines = UBound(splitString) + 1

    If nLines <= nLinesToDisplay Then
        'No need to remove anything. Get out.
        GetLastLines = s
        Exit Function
    End If

    'Collect last N lines in a new array
    Dim lastLines() As String
    ReDim lastLines(0 To nLinesToDisplay - 1)
    Dim i As Long
    For i = 0 To UBound(lastLines)
        lastLines(i) = splitString(i + nLines - nLinesToDisplay)
    Next i

    'Join the lines array into a single string
    GetLastLines = Join(lastLines, lineBreakChar)
End Function

使用示例:

MsgBox GetLastLines( _
    "line 1" & vbCrLf & "line 2" & vbCrLf & "line 3" & vbCrLf _
        & "line 4" & vbCrLf & "line 5" & vbCrLf & "line 6", _
    4, vbCrLf)

仅显示最后4行:

enter image description here

请注意,这假设您的最后一行由换行符终止。如果是,那么你可以调整代码来处理它。

或者,您可以使用Excel的内置SUBSTITUTE函数,这在此特定情况下很有用,因为它可以找到给定字符的特定实例。因此,您可以使用单行代码来代替构建数组:

Function GetLastLines2(ByVal s As String, ByVal nLinesToDisplay As Long, _
    Optional ByVal lineBreakChar As String = vbCrLf)

    'An arbitrary character that will never be in your input string:
    Dim delim As String: delim = Chr(1)

    'How many lines are there?
    Dim nLines As Long
    nLines = UBound(Split(s, lineBreakChar)) + 1

    If nLines <= nLinesToDisplay Then
        'No need to remove anything. Get out.
        GetLastLines2 = s
        Exit Function
    End If

    'Replace one line break with delim, split the string on it, 
    'return only second part:
    GetLastLines2 = Split( _
        WorksheetFunction.Substitute( _
            s, lineBreakChar, delim, nLines - nLinesToDisplay), _
        delim)(1)

End Function

答案 1 :(得分:0)

A = "Cat" & vbcrlf & "Tiger" & vbcrlf & "Lion" & vbcrlf & "Shark hunting florida lynxs" & vbcrlf & "Leopard" & vbcrlf & "Cheetah"
A= StrReverse(A)
NumLines = 3
i=1
For X = 1 to NumLines
    i = Instr(i, A, vbcr) + 1
Next

Msgbox StrReverse(Left(A, i - 1))

这是一个从文件的顶部或底部剪切或留下行的程序。

使用

<强>剪切

filter cut {t|b} {i|x} NumOfLines

从文件的顶部或底部剪切行数。

t - top of the file
b - bottom of the file
i - include n lines
x - exclude n lines

示例

cscript //nologo filter.vbs cut t i 5 < "%systemroot%\win.ini"

脚本

    Set rs = CreateObject("ADODB.Recordset")
    With rs
        .Fields.Append "LineNumber", 4 
        .Fields.Append "Txt", 201, 5000 
        .Open
        LineCount = 0
        Do Until Inp.AtEndOfStream
            LineCount = LineCount + 1
            .AddNew
            .Fields("LineNumber").value = LineCount
            .Fields("Txt").value = Inp.readline
            .UpDate
        Loop

        .Sort = "LineNumber ASC"

        If LCase(Arg(1)) = "t" then
            If LCase(Arg(2)) = "i" then
                .filter = "LineNumber < " & LCase(Arg(3)) + 1
            ElseIf LCase(Arg(2)) = "x" then
                .filter = "LineNumber > " & LCase(Arg(3))
            End If
        ElseIf LCase(Arg(1)) = "b" then
            If LCase(Arg(2)) = "i" then
                .filter = "LineNumber > " & LineCount - LCase(Arg(3))
            ElseIf LCase(Arg(2)) = "x" then
                .filter = "LineNumber < " & LineCount - LCase(Arg(3)) + 1
            End If
        End If

        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
            .MoveNext
        Loop
    End With