Limiting a conditional message box to pop up only once?

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have some VBA code that highlights cells which exceed a certain number of characters 'Red'--- until the cell character count drops below the condition, then the cell becomes un-highlighted.

At the end of the VBA I put a message box to inform the user their cells exceed character limits. This message box currently pops up every time there is a change to the cell contents, until all the cells referred to in the code meet the conditions.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim c As Range
Dim LR As Integer
Dim numProbs As Long
Dim sht As Worksheet


Set sht = Worksheets("DX import template")


numProbs = 0
LR = sht.Cells(Rows.Count, "A").End(xlUp).Row
For Each c In Range("A2:A" & LR)
    If Len(c.Value) > 12 Then
        c.Interior.Color = vbRed
        numProbs = numProbs + 1
    Else
      c.Interior.Color = xlNone
    End If
Next
For Each c In Range("B2:B" & LR)
    If Len(c.Value) > 20 Then
        c.Interior.Color = vbRed
        numProbs = numProbs + 1
    Else
      c.Interior.Color = xlNone
    End If
Next
For Each c In Range("C2:C" & LR)
    If Len(c.Value) > 50 Then
        c.Interior.Color = vbRed
        numProbs = numProbs + 1
    Else
      c.Interior.Color = xlNone
    End If

[COLOR=#0000cd][B]If numProbs > 0 Then[/B][/COLOR][COLOR=#0000cd][B]    MsgBox "Character limits - Columns: A (12), B (20), C (50) see " & numProbs & " red cells"[/B][/COLOR]

End If


End Sub


What I want this code to do is to activate the pop-up box only once if any of the cells contain more than the permitted characters, so that it's not necessary to keep closing the pop-up box after each amendment. I googled this without success.

Would anybody be able to help me amend this code please?

Kind regards,

Doug.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,
try moving your msgbox outside the for next loop

Rich (BB code):
For Each c In Range("C2:C" & LR)
    If Len(c.Value) > 50 Then
        c.Interior.Color = vbRed
        numProbs = numProbs + 1
    Else
      c.Interior.Color = xlNone
    End If


End If
Next


If numProbs > 0 Then MsgBox "Character limits - Columns: A (12), B (20), C (50) see " & numProbs & " red cells"

Dave
 
Upvote 0
Hi Dave,

Thanks for answering.

If I move the message box code outside as shown in your code window, I get the following error:

Code:
Compile error: End If without block If

I tried this though based on what you said:
Code:
For Each c In Range("P2:P" & LR)    If Len(c.Value) > 10 Then
        c.Interior.Color = vbRed
        numProbs = numProbs + 1
    Else
      c.Interior.Color = xlNone
    End If
Next
If numProbs > 0 Then
    MsgBox "Character limits - Columns: A (12), B (20), C to G (50), H & I (20), J & K (8), L (12), M (5), N(3), O & P (10) see " & numProbs & " red cells"
End If


End Sub

This gave me the same result: every red cell I altered to below the character limit, it went un-highlighted but then the msgbox popped up.

Kind regards,

Doug
 
Upvote 0
First, sorry about that my bad, forgot to delete the Endif

If you are just testing for character length the try using the Text property of Range and see if this helps

Rich (BB code):
If Len(c.Text) > 10 Then

Dave
 
Last edited:
Upvote 0
First, sorry about that my bad, forgot to delete the Endif

If you are just testing for character length the try using the Text property of Range and see if this helps

Rich (BB code):
If Len(c.Text) > 10 Then

Dave

Hi Dave,

This works better, thanks!

Kind regards,

Doug.
 
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,728
Members
449,332
Latest member
nokoloina

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top