MsgBox doesn't go away

kitkatjam

New Member
Joined
Nov 30, 2018
Messages
11
I have a macro that looks up a number and copies the information if it's an old account.
The first msgbox displays perfectly; it pops up, I hit "ok" and it continues the code and moves to the next iteration. The next message box, however, continues to pop up when I hit "ok" and I don't know why it won't move to the next iteration. The layout looks the same to me, the iterative piece is on the outside of both "End If" statements, right before End Sub, so it seems as though it should move through correctly.


Code:
Do While Cells(i, 1).Value <> ""    
    If IP.Range("B" & i) = "New" Then
        MsgBox "Account is 'new'. Please enter account details."
    Else
    
        Set Num = IP.Range("A" & i)
        Num.Copy
        OP.Activate
        x = WorksheetFunction.CountIf(Range("C:C"), Num.Value)  
        If x = 0 Then
            MsgBox "Number " & Num & "was not found. Please check the number and try again."
        Else



I'm not sure if this is clear, but any help would be greatly appreciated because I'm not sure what I'm doing wrong!
Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Can you post the rest of the code please?
 
Upvote 0
Code:
Cvg.ActivateRange("A4").Select
Rows(ActiveCell.row & ":" & Rows.Count).Delete


IP.Activate
Do While Cells(i, 1).Value <> ""   
    If IP.Range("B" & i) = "New" Then
        MsgBox "Account is 'new'. Please enter account details."
    Else
    
        Set Num = IP.Range("A" & i)
        Num.Copy
        OP.Activate
        x = WorksheetFunction.CountIf(Range("C:C"), Num.Value) 
        If x = 0 Then
            MsgBox "Number " & Num & "was not found. Please check the number and try again."
        Else
            row = Application.WorksheetFunction.Match(Num, Range("C:C"), 0)  
            r = row + x - 1 'the row where the number last appears
            On Error GoTo 0
                If row > 0 Then
                Range("A" & row & ":AE" & r).Copy
                End If
            Cvg.Activate
            Range("A" & j).PasteSpecial xlPasteValues
        
            s = j + x 'one row below last line for total
            t = s - 1 'last line
            Set inc = Cvg.Range("AC" & j & ":AC" & t)
            Set exc = Cvg.Range("AD" & j & ":AD" & t)
            Range("Q" & s).Value = Application.WorksheetFunction.SumIfs(Range("Q" & j & ":Q" & t), inc, "Include", exc, "") 
            Range("R" & s).Value = Application.WorksheetFunction.SumIfs(Range("R" & j & ":R" & t), inc, "Include", exc, "") 
            Range("S" & s).Value = Application.WorksheetFunction.SumIfs(Range("S" & j & ":S" & t), inc, "Include", exc, "")
            Range("T" & s).Value = Application.WorksheetFunction.SumIfs(Range("T" & j & ":T" & t), inc, "Include", exc, "")
            Range("U" & s).Value = Application.WorksheetFunction.SumIfs(Range("U" & j & ":U" & t), inc, "Include", exc, "")

        
            Range("A" & t).Copy
            IP.Activate
            Range("C" & i).PasteSpecial xlPasteValues   
            Cvg.Activate
            Range("F" & t).Copy
            IP.Activate
            Range("D" & i).PasteSpecial xlPasteValues  
            Cvg.Activate
            Range("I" & t).Copy
            IP.Activate
            Range("E" & i).PasteSpecial xlPasteValues  




            j = s + 2
    
        End If
         
    End If
    
i = i + 1
Loop
    
        
        
End Sub
 
Upvote 0
I meant the full code. What routine is this and where is the code located? (which module)
 
Upvote 0
A routine is the generic name for a Sub or Function.
 
Upvote 0
How do you know it is not moving on to the next iteration? I suspect that you do not have a msgbox problem, you have an x = 0 problem. If the 2nd msgbox keeps popping up repeatedly

Code:
        If x = 0 Then
            MsgBox "Number " & Num & "was not found. Please check the number and try again."
        Else

it more likely because x = 0 for each iteration. You can test this by using the debugger to single step through the code.
 
Upvote 0
How do you know it is not moving on to the next iteration? I suspect that you do not have a msgbox problem, you have an x = 0 problem. If the 2nd msgbox keeps popping up repeatedly

Code:
        If x = 0 Then
            MsgBox "Number " & Num & "was not found. Please check the number and try again."
        Else

it more likely because x = 0 for each iteration. You can test this by using the debugger to single step through the code.


When I stepped through the code, it worked perfectly. It read that x=0, popped up the message box, I hit "OK" and it jumped to End If, then iterated; since there was nothing in the next line, it then jumped to exit sub. It worked exactly as I would think it should work, but when I run the code without stepping through it piecemeal, it wouldn't stop popping up with the MsgBox.
 
Upvote 0
Oh I think I see what you mean - I think I misspoke. It doesn't look like it is actually exiting the sub. My IP.Activate needed to be hit again, so it was looking in the wrong tab to see if the line was blank or not, and then just running for probably 20,000 lines until it died out.
THANK YOU SO MUCH! That was amazing.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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