Debug.Print Causes Crash

  • Hi,


    Continuing with my goal to learn VBA and while playing with the following code, I encountered strange behavior of debug statements. In the code below I have put my comments to identify where the "funny behavior" is encountered.


    Will appreciate if you can help me understand. I also have a simple request/question at the end of the code below:



    Also, what i want to do is that at the end, the very next cell (i.e. 1 row down) of Rng1 and Rng2 should be filled with heavy border and Red fill.


    Thanks.

  • Re: Weird Behavior Of Debug Statements


    a possibility could be:


    in the first case




    filippo

  • Re: Weird Behavior Of Debug Statements


    the objects r1 and r2 are loop objects. Once the loop is complete they reference nothing.


    If you are try to find the last cell in the ranges Rng1 and Rg2 use


    Code
    Debug.Print "R1.Value = " & Rng1.Cells(Rng1.Rows.Count, 1).Value
    Debug.Print "R1.Address = " & Rng1.Cells(Rng1.Rows.Count, 1).Address
    Debug.Print "R2.Value = " & Rng2.Cells(Rng2.Rows.Count, 1).Value
    Debug.Print "R2.Address = " & Rng2.Cells(Rng2.Rows.Count, 1).Address


    You might find using some built in functions quicker than looping through every cell evrey time.


    [h4]Cheers
    Andy
    [/h4]

  • Re: Weird Behavior Of Debug Statements


    Hi Andy,


    I went through your code in detail and learnt quite a lot of new things. Thanks :)


    There is one problem however. I am trying to identify and find a fix but given my current VBA skills this will probably take a long time. So I will grateful if you can help. The problem is that your code higlights the cells in Sheet1 that are not in Sheet2. What confuses me is that though the code to do so is there (please see the code below)H but it does not do the same for Sheet2 i.e. higlight the cells in Sheet2 that are NOT in Sheet1)



    Code
    For Each r2 In Rng2.Cells 
            If IsError(Application.WorksheetFunction.Match(r2.Value, Rng1, 0)) Then 
                r1.Interior.ColorIndex = 6 
            End If 
    Next



    Another observation (i may be making some stupid mistake some where) is that in each iteration, the debug statement in the following code are printing the same results??? :confused:



    Code
    On Error Resume Next
    For Each r1 In Rng1.Cells
    'Debug.Print "R1.Value = " & Rng1.Cells(Rng1.Rows.Count, 1).Value
    'Debug.Print "R1.Address = " & Rng1.Cells(Rng1.Rows.Count, 1).Address
        If IsError(Application.WorksheetFunction.Match(r1.Value, Rng2, 0)) Then
            r1.Interior.ColorIndex = 6
        End If
    Next


    Last question: Should I also put a "On Error Resume Next line" at the start of the 2nd for loop (i.e. the 1st piece of code that I have pasted above).



    Advance apologies for the botheration.




    Thanks.

  • Re: Weird Behavior Of Debug Statements


    Looks like a typo by me.


    On the second test change the reference from r1 to r2


    Code
    For Each r2 In Rng2.Cells 
            If IsError(Application.WorksheetFunction.Match(r2.Value, Rng1, 0)) Then 
                r2.Interior.ColorIndex = 6 
            End If 
        Next


    The on error is not required twice.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Weird Behavior Of Debug Statements


    Andy thanks again for the quick reply.
    No, your original quote was correct and no need to change the reference from r1 to r2. Unless I'm making a mistake, there seems to be a problem in the following piece of code


    Code
    For Each r1 In Rng1.Cells 
            If IsError(Application.WorksheetFunction.Match(r1.Value, Rng2, 0)) Then 
                r1.Interior.ColorIndex = 6 
            End If


    What the code is supposed to do is to highlight (with yellow fill) each cell in each sheet which does NOT exists in the other sheet. Your code above sems to be be doing the opposite??


    I'm confused :(

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!