2017-10-18 81 views
0

我有一個循環查找列中的文本(即工作),我想將結果發佈在MsgBox中,但是當我在循環中或循環外使用msgbox時, msgbox找到的每個結果或只有一個msgbox帶有一個結果。我想讓它在每個結果後在1 msgbox中發送每個結果並帶有換行符。循環結果發佈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 

與MSGBOX循環:

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 

回答

2

你需要堅持的txt舊值。

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