【问题标题】:Loop result post Msgbox new line循环结果发布 Msgbox 新行
【发布时间】:2017-10-18 03:50:40
【问题描述】:

我有一个循环在列中查找文本(正在工作),我想将结果发布到 MsgBox 中,但是当我在循环内或循环外使用 msgbox 时,我会为找到的每个结果获得一个 msgbox 或只有一个 msgbox 有一个结果。我想要的是让它在 1 个 msgbox 中发布每个结果,每个结果后都有一个换行符。

我知道第一个代码不是寻找重复项的最漂亮或最好的方法,我应该为它使用一个数组,但这是我让它工作的唯一方法。

第一个发现重复的代码(与问题无关):

Dim lastRow As Long
Dim i As Long
Dim ws As Worksheet
Dim txt As String
Set ws = Sheets("Player List")
Dim matchFoundIndex As Long
Dim iCntr As Long

lastRow = Range("A201").End(xlUp).Row
For iCntr = 1 To lastRow
    If Cells(iCntr, 1) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & 
        lastRow), 0)
        If iCntr <> matchFoundIndex Then
            Cells(iCntr, 2) = "Duplicate"
        End If
    End If
Next

带有消息框的循环:

For i = 2 To 201
    If ws.Range("B" & i).Value = "Duplicate" Then
        txt = "Duplicates found for" + " " + ws.Range("A" & i).Value + " " + "in" + 
            ws.Range("L" & i).Value + vbNewLine
        End If
    Next i
MsgBox txt

【问题讨论】:

    标签: vba loops msgbox


    【解决方案1】:

    您需要保留 txt 的旧值。

    txt = txt & "Duplicates found for" & " " & ws.Range("A" & i).Value & " " & "in" & ws.Range("L" & i).Value & vbNewLine
    

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2014-07-17
      • 2019-04-22
      • 2012-08-10
      • 2021-11-16
      • 2018-07-27
      相关资源
      最近更新 更多