Consulting

Results 1 to 4 of 4

Thread: One message box in a for loop

  1. #1

    One message box in a for loop

    I have a for loop :

    Sub color2()
    Dim N As Long
    For N = 1 To 56
    If Cells(N, 16).Interior.ColorIndex = 3 Then
    MsgBox "Codes don't Exist "
    End If
    Next N


    End Sub


    this code gives me message box for every cell which has color 3 (red)
    i want just one message box, for all the cells in red

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    [vba]Sub color2_1()
    Dim N As Long
    Dim Temp$
    For N = 1 To 5
    If Cells(N, 16).Interior.ColorIndex = 3 Then
    Temp$ = Temp$ & N & vbCr
    End If
    Next N

    MsgBox "Rows" & vbCr & Temp$ & "Don't have codes."

    End Sub
    [/vba]

    You really should have added this to the original question.

    David


  3. #3
    Thank you very much, but this code is giving me the message box when i don't have red cells, i need message box just when i have red cells

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]
    Sub color2()
    Dim N As Long, s As String
    s = ""
    For N = 1 To 56
    If Cells(N, 16).Interior.ColorIndex = 3 Then
    s = s & Cells(N, 16).Address & vbLf
    End If
    Next N
    If s = "" Then
    MsgBox "P1 to P56 has no interior color red."
    Else: MsgBox "Cells with interior color red:" & vbLf & s
    End If
    End Sub[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •