VBA code that returns the results of macro in a MsgBox

KrystynaD

New Member
Joined
Feb 11, 2020
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
At the end of running my macro I would really like it if a message box appeared displaying the results of the macro.
The message box would say "X" number discrepancies were identified, or "0" discrepancies were identified.
I am having great difficulty making this happen, i would really appreciate the help.

Please see my code below..

- This code compares the data in Sheet1 and Sheet 2 looking for any discrepancies.
- The results from this macro are then returned in Sheet 3.
- I have attached 3 images. Sheet1 and Sheet2 are the data being compared and sheet 3 is the result of the comparisons - showing any discrepancy.


VBA Code:
Sub LookForDiscrepancies()
    Dim varS1, varS2, varH1, varH2
    Dim rngS1 As Range, rngS2 As Range
    Dim c As Range, c1 As Range, c2 As Range
    Dim iRow As Integer, iCol As Integer, i As Integer, iTest As Integer
    
    Sheet1.Activate
    Set rngS1 = Intersect(Sheet1.UsedRange, Columns("A"))
    Sheet2.Activate
    Set rngS2 = Intersect(Sheet2.UsedRange, Columns("A"))
    Sheet3.Activate
    Sheet3.Cells.Select
    Ans = MsgBox(prompt:="Do you really want to delete data in Sheet3?", _
Buttons:=vbYesNo + vbExclamation, _
Title:="Delete Data!")

If Ans = vbNo Then Exit Sub ' vbNo = 7, vbYes = 6


    Selection.Delete Shift:=xlUp
    Sheet3.Rows("1:1").Value = Sheet1.Rows("1:1").Value
    
    Let iRow = iRow + 2
    With rngS2
         'Search for Sheet1 IDs on Sheet2
        For Each c1 In rngS1
            On Error GoTo 0
            Set c = .Find(what:=c1.Value) 'Look for match
            If c Is Nothing Then 'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c1
                Sheet3.Cells(iRow, 2) = "exist in sheet 1 not in sheet 2"
                Let iRow = iRow + 1
                Else 'Check if rows are identical
                Let varS1 = Intersect(Sheet1.UsedRange, c1.EntireRow)
                Let varS2 = Intersect(Sheet2.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet1.UsedRange, c1.EntireRow).Count
                ReDim varH1(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH1(i) = 1
                    End If
                Next i
                If iTest Then 'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH1(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 40
                    Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                End If
            End If
        Next
    End With
    
    Let iRow = iRow + 0
    Range("A1").Offset(iRow, 0).Value = "Sheet2 vs Sheet1"
    Let iRow = iRow + 2
        With rngS1
         'Search for Sheet2 IDs on Sheet1
        For Each c2 In rngS2
            On Error GoTo 0
            Set c = .Find(what:=c2.Value) 'Look for match
            If c Is Nothing Then 'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c2
                Sheet3.Cells(iRow, 2) = "exist in sheet 2 not in sheet 1"
                Let iRow = iRow + 1
                Else 'Check if rows are identical
                Let varS1 = Intersect(Sheet2.UsedRange, c2.EntireRow)
                Let varS2 = Intersect(Sheet1.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet2.UsedRange, c2.EntireRow).Count
                ReDim varH2(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH2(i) = 1
                    End If
                Next i
                If iTest Then 'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH2(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 36
                        Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                    End If
            End If
        Next
    End With
     Sheet3.Select 'resize the columns
     Range("A:Z").Columns.AutoFit
         
End Sub
 

Attachments

  • Screenshot 2020-03-15 at 17.13.48.png
    Screenshot 2020-03-15 at 17.13.48.png
    152.5 KB · Views: 22
  • Screenshot 2020-03-15 at 17.12.32.png
    Screenshot 2020-03-15 at 17.12.32.png
    233.8 KB · Views: 26
  • Screenshot 2020-03-15 at 17.21.45.png
    Screenshot 2020-03-15 at 17.21.45.png
    222.3 KB · Views: 21
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Your post is not too clear about the specific information you need to display in the msgbox, so I'm making some guesses to show you a general method that you can use to tailor to your liking.
VBA Code:
Sub LookForDiscrepancies()

    Dim varS1, varS2, varH1, varH2
    Dim rngS1 As Range, rngS2 As Range
    Dim c As Range, c1 As Range, c2 As Range
    Dim iRow As Integer, iCol As Integer, i As Integer, iTest As Integer
    Dim Msg As String, Title As String, Ans As Integer

    Title = "Discrepancies"
    Msg = ""

    Sheet1.Activate
    Set rngS1 = Intersect(Sheet1.UsedRange, Columns("A"))
    Sheet2.Activate
    Set rngS2 = Intersect(Sheet2.UsedRange, Columns("A"))
    Sheet3.Activate
    Sheet3.Cells.Select
    Ans = MsgBox(prompt:="Do you really want to delete data in Sheet3?", _
                 Buttons:=vbYesNo + vbExclamation, _
                 Title:="Delete Data!")

    If Ans = vbNo Then Exit Sub                       ' vbNo = 7, vbYes = 6

    Selection.Delete Shift:=xlUp
    Sheet3.Rows("1:1").Value = Sheet1.Rows("1:1").Value

    Let iRow = iRow + 2
    With rngS2
        'Search for Sheet1 IDs on Sheet2
        For Each c1 In rngS1
            On Error GoTo 0
            Set c = .Find(what:=c1.Value)             'Look for match
            If c Is Nothing Then                      'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c1
                Sheet3.Cells(iRow, 2) = "exist in sheet 1 not in sheet 2"
                Msg = Msg & c1.Value & " exists in sheet 1, but not in sheet 2" & vbCr
                Let iRow = iRow + 1
            Else                                      'Check if rows are identical
                Let varS1 = Intersect(Sheet1.UsedRange, c1.EntireRow)
                Let varS2 = Intersect(Sheet2.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet1.UsedRange, c1.EntireRow).Count
                ReDim varH1(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH1(i) = 1
                    End If
                Next i
                If iTest Then                         'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH1(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 40
                        Msg = Msg & "Sheet1 IDs on Sheet2: " & Cells(iRow, i).Address & vbCr
                    Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                End If
            End If
        Next
    End With

    Let iRow = iRow + 0
    Range("A1").Offset(iRow, 0).Value = "Sheet2 vs Sheet1"
    Let iRow = iRow + 2
    With rngS1
        'Search for Sheet2 IDs on Sheet1
        For Each c2 In rngS2
            On Error GoTo 0
            Set c = .Find(what:=c2.Value)             'Look for match
            If c Is Nothing Then                      'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c2
                Sheet3.Cells(iRow, 2) = "exist in sheet 2 not in sheet 1"
                Msg = Msg & c2.Value & " exists in sheet 2,but not in sheet 1" & vbCr
                Let iRow = iRow + 1
            Else                                      'Check if rows are identical
                Let varS1 = Intersect(Sheet2.UsedRange, c2.EntireRow)
                Let varS2 = Intersect(Sheet1.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet2.UsedRange, c2.EntireRow).Count
                ReDim varH2(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH2(i) = 1
                    End If
                Next i
                If iTest Then                         'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH2(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 36
                        Msg = Msg & "Sheet2 IDs on Sheet1: " & Cells(iRow, i).Address & vbCr
                    Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                End If
            End If
        Next
    End With
    Sheet3.Select                                     'resize the columns
    Range("A:Z").Columns.AutoFit
    If Msg <> "" Then
        Msg = "Discrepancies Found:" & vbCr & vbCr & Msg
        MsgBox Msg, vbExclamation, Title
    End If
End Sub
 
Upvote 0
Thank you for your post & apologies for not making it clear. Ideally I would like the message box to display whether any discrepancies were identified in sheets 1 and sheet 2.

The data in sheet 1&2 should be identical, so when the macro is run the results in sheet 3 should show no data because there is no discrepancy. Now, if there are discrepancies between the data in sheet 1 and sheet 2, sheet 3 will show those discrepancies and highlight them. (I have uploaded the image of Sheet 3 to show what that looks like. The data highlighted in Orange, at the top, is the correct data from sheet 1, and the data highlighted in Yellow, under the title Sheet 2 V Sheet 1, is the data with the discrepancies in sheet 2). Once the macro has been run i would like a message box at the end that returns the result of "x" discrepancies were identified (if we use the example of sheet 3 - the uploaded image- the message box would say 2 discrepancies were identified, but if there were no discrepancies identified between sheet 1 and sheet 2 then the message box would say no discrepancies were identified).

Hope this explanation helps.

I tired the added code you sent over and the result i get back is different ( i get what you meant when you said it wasn't clear) (i have uploaded an image of the result).

Would really appreciate the help.
 

Attachments

  • Screenshot 2020-03-15 at 17.13.48.png
    Screenshot 2020-03-15 at 17.13.48.png
    152.5 KB · Views: 23
  • Screenshot 2020-03-15 at 19.48.35.png
    Screenshot 2020-03-15 at 19.48.35.png
    84 KB · Views: 23
Upvote 0
So a couple of things.

1. You are asking for help with putting the results of the macro in a message box, not with code to identify discrepancies.
2. The macro code to identify discrepancies already exists and you have posted it.

Getting a msgbox to display data is very easy. The challenge in this case is for you to identify the lines of your code which already place the discrepancies in sheet 3 and then place that same information in a message box. This is what I understand about that from your description:

1. The orange cells are the correct data
2. The Yellow cells are the discrepancies.

On that basis, counting the cells you set to yellow (ColorIndex = 36) should produce the number of discrepancies.
VBA Code:
Sub LookForDiscrepancies()

    Dim varS1, varS2, varH1, varH2
    Dim rngS1 As Range, rngS2 As Range
    Dim c As Range, c1 As Range, c2 As Range
    Dim iRow As Integer, iCol As Integer, i As Integer, iTest As Integer
    Dim Title As String, Ans As Integer, DCnt As Integer

    Title = "Discrepancies"
    DCnt = 0

    Sheet1.Activate
    Set rngS1 = Intersect(Sheet1.UsedRange, Columns("A"))
    Sheet2.Activate
    Set rngS2 = Intersect(Sheet2.UsedRange, Columns("A"))
    Sheet3.Activate
    Sheet3.Cells.Select
    Ans = MsgBox(prompt:="Do you really want to delete data in Sheet3?", _
                 Buttons:=vbYesNo + vbExclamation, _
                 Title:="Delete Data!")

    If Ans = vbNo Then Exit Sub                       ' vbNo = 7, vbYes = 6

    Selection.Delete Shift:=xlUp
    Sheet3.Rows("1:1").Value = Sheet1.Rows("1:1").Value

    Let iRow = iRow + 2
    With rngS2
        'Search for Sheet1 IDs on Sheet2
        For Each c1 In rngS1
            On Error GoTo 0
            Set c = .Find(what:=c1.Value)             'Look for match
            If c Is Nothing Then                      'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c1
                Sheet3.Cells(iRow, 2) = "exist in sheet 1 not in sheet 2"
                Let iRow = iRow + 1
            Else                                      'Check if rows are identical
                Let varS1 = Intersect(Sheet1.UsedRange, c1.EntireRow)
                Let varS2 = Intersect(Sheet2.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet1.UsedRange, c1.EntireRow).Count
                ReDim varH1(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH1(i) = 1
                    End If
                Next i
                If iTest Then                         'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH1(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 40
                    Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                End If
            End If
        Next
    End With

    Let iRow = iRow + 0
    Range("A1").Offset(iRow, 0).Value = "Sheet2 vs Sheet1"
    Let iRow = iRow + 2
    With rngS1
        'Search for Sheet2 IDs on Sheet1
        For Each c2 In rngS2
            On Error GoTo 0
            Set c = .Find(what:=c2.Value)             'Look for match
            If c Is Nothing Then                      'Copy the ID to Sheet3
                Sheet3.Cells(iRow, 1) = c2
                Sheet3.Cells(iRow, 2) = "exist in sheet 2 not in sheet 1"
                Let iRow = iRow + 1
            Else                                      'Check if rows are identical
                Let varS1 = Intersect(Sheet2.UsedRange, c2.EntireRow)
                Let varS2 = Intersect(Sheet1.UsedRange, c.EntireRow)
                Let iCol = Intersect(Sheet2.UsedRange, c2.EntireRow).Count
                ReDim varH2(1 To iCol) As Integer
                For i = 1 To iCol
                    If Not varS1(1, i) = varS2(1, i) Then
                        Let iTest = iTest + 1
                        Let varH2(i) = 1
                    End If
                Next i
                If iTest Then                         'Rows are not identical
                    For i = 1 To iCol
                        Sheet3.Cells(iRow, i) = varS1(1, i)
                        If Not varH2(i) = 0 Then Cells(iRow, i) _
                        .Interior.ColorIndex = 36
                        DCnt = DCnt + 1               'you said the yellow cells are the discrepancies, so let's count the yellow cells.
                    Next i
                    Let iTest = 0
                    Let iRow = iRow + 1
                End If
            End If
        Next
    End With
    Sheet3.Select                                     'resize the columns
    Range("A:Z").Columns.AutoFit
    If DCnt > 0 Then
        MsgBox DCnt & " discrepancies found", vbExclamation, Title
    Else
        MsgBox "No discrepancies found",vbInformation , Title
    End If
End Sub
 
Upvote 0
Thank you for taking the time to respond to me again.

First, yes you are correct, i am looking for help with putting the results of the macro in a message box. Ideally once the macro has finished running a message box would appear saying either "Discrepancies have been identified or No Discrepancies have been identified", based on the result of the macro.

When you said "On that basis, counting the cells you set to yellow (ColorIndex = 36) should produce the number of discrepancies." I like that idea of counting the number of yellow cells to produce the number of discrepancies, this makes a lot of sense. However when I run the macros with the added code you sent over it keeps on saying "No discrepancies found" despite there being some discrepancies ( I included an image for reference). Im not sure why that is.

Do you think i am missing something?
 

Attachments

  • Screenshot 2020-03-15 at 22.41.34.png
    Screenshot 2020-03-15 at 22.41.34.png
    203.1 KB · Views: 14
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.
Be sure to follow & read the link at the end of the rule too!


If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Apologies, i just realised i did something wrong. I tried the added code again and it worked. However instead of it counting just the number of Yellow cells to produce the number of discrepancies in the message box, it is counting all the cells with data under the Sheet2 Vs Sheet1 title
Screenshot 2020-03-15 at 23.41.14.png
(i think the Dnct function is counting all the cells under the Sheet 2 Vs Sheet 1 title). In the image uploaded there are only 2 discrepancies that are highlighted in Yellow under the Sheet2 Vs Sheet 1 title. However the message box returns with the result of 12 discrepancies because it has counted all the cells with data under the Sheet2 Vs Sheet 1title. is there a way that i could just get it to show the number of discrepancies just in Yellow?
 
Upvote 0
Thank you for taking the time to respond to me again.

First, yes you are correct, i am looking for help with putting the results of the macro in a message box. Ideally once the macro has finished running a message box would appear saying either "Discrepancies have been identified or No Discrepancies have been identified", based on the result of the macro.

When you said "On that basis, counting the cells you set to yellow (ColorIndex = 36) should produce the number of discrepancies." I like that idea of counting the number of yellow cells to produce the number of discrepancies, this makes a lot of sense. However when I run the macros with the added code you sent over it keeps on saying "No discrepancies found" despite there being some discrepancies ( I included an image for reference). Im not sure why that is.

Do you think i am missing something?

PLEASE DISREGARD THIS MESSAGE. I DID SOMETHING WRONG HERE.
 
Upvote 0
...However instead of it counting just the number of Yellow cells to produce the number of discrepancies in the message box, it is counting all the cells with data under the Sheet2 Vs Sheet1 title...

I'm not sure how that's possible unless your actual code is different from the code you posted. I only made one very minor modification which was to add DCnt = DCnt +1 just after the place you set .Interior.ColorIndex = 36 so
VBA Code:
                        For i = 1 To iCol
                            Sheet3.Cells(iRow, i) = varS1(1, i)
                            If Not varH2(i) = 0 Then Cells(iRow, i) _
                            .Interior.ColorIndex = 36     'this is the only place in your posted code that sets the color to yellow.
                            DCnt = DCnt + 1               'you said the yellow cells are the discrepancies, so let's count the yellow cells.
                        Next i

I'm not sure from your posted code how the number of yellow cells could be different from DCnt. Have you tried to use the debugger to set a breakpoint at
VBA Code:
         .Interior.ColorIndex = 36     'this is the only place in your posted code that sets the color to yellow.
so that you can watch the cells turn yellow one by one and inspect the value of DCnt?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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