Compare value of cells of a column in VBA












0















I have data in a column as follows:



8856

8867

8876

8856

8898



My objective is to compare each cell of the column and if the values are the same to execute an if statement.



Private Sub CommandButton2_Click()

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim C1row As Long
Dim C2row As Long
Dim C2TotalRows As Long
Dim CustID As String

Set sht1 = Worksheets("Report")

sht1.Activate
C2TotalRows = Application.CountA(Range("A:A"))
C1row = 2

Do While sht1.Cells(C1row, 3).Value <> ""

CustID = sht1.Cells(C1row, 3).Value

For C2row = 2 To C2TotalRows

If CustID = Cells(C2row, 3).Value Then
MsgBox CustID
Exit For
End If

Next
C1row = C1row + 1

Loop
End Sub









share|improve this question




















  • 1





    What's a problem exactly, what don't work?

    – slesh
    Jan 1 at 9:10











  • i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

    – Noob_247
    Jan 1 at 9:16













  • what are you planning to do with the duplicate?

    – GMalc
    Jan 1 at 21:17
















0















I have data in a column as follows:



8856

8867

8876

8856

8898



My objective is to compare each cell of the column and if the values are the same to execute an if statement.



Private Sub CommandButton2_Click()

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim C1row As Long
Dim C2row As Long
Dim C2TotalRows As Long
Dim CustID As String

Set sht1 = Worksheets("Report")

sht1.Activate
C2TotalRows = Application.CountA(Range("A:A"))
C1row = 2

Do While sht1.Cells(C1row, 3).Value <> ""

CustID = sht1.Cells(C1row, 3).Value

For C2row = 2 To C2TotalRows

If CustID = Cells(C2row, 3).Value Then
MsgBox CustID
Exit For
End If

Next
C1row = C1row + 1

Loop
End Sub









share|improve this question




















  • 1





    What's a problem exactly, what don't work?

    – slesh
    Jan 1 at 9:10











  • i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

    – Noob_247
    Jan 1 at 9:16













  • what are you planning to do with the duplicate?

    – GMalc
    Jan 1 at 21:17














0












0








0








I have data in a column as follows:



8856

8867

8876

8856

8898



My objective is to compare each cell of the column and if the values are the same to execute an if statement.



Private Sub CommandButton2_Click()

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim C1row As Long
Dim C2row As Long
Dim C2TotalRows As Long
Dim CustID As String

Set sht1 = Worksheets("Report")

sht1.Activate
C2TotalRows = Application.CountA(Range("A:A"))
C1row = 2

Do While sht1.Cells(C1row, 3).Value <> ""

CustID = sht1.Cells(C1row, 3).Value

For C2row = 2 To C2TotalRows

If CustID = Cells(C2row, 3).Value Then
MsgBox CustID
Exit For
End If

Next
C1row = C1row + 1

Loop
End Sub









share|improve this question
















I have data in a column as follows:



8856

8867

8876

8856

8898



My objective is to compare each cell of the column and if the values are the same to execute an if statement.



Private Sub CommandButton2_Click()

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim C1row As Long
Dim C2row As Long
Dim C2TotalRows As Long
Dim CustID As String

Set sht1 = Worksheets("Report")

sht1.Activate
C2TotalRows = Application.CountA(Range("A:A"))
C1row = 2

Do While sht1.Cells(C1row, 3).Value <> ""

CustID = sht1.Cells(C1row, 3).Value

For C2row = 2 To C2TotalRows

If CustID = Cells(C2row, 3).Value Then
MsgBox CustID
Exit For
End If

Next
C1row = C1row + 1

Loop
End Sub






excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 21:37









Community

11




11










asked Jan 1 at 9:04









Noob_247Noob_247

11




11








  • 1





    What's a problem exactly, what don't work?

    – slesh
    Jan 1 at 9:10











  • i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

    – Noob_247
    Jan 1 at 9:16













  • what are you planning to do with the duplicate?

    – GMalc
    Jan 1 at 21:17














  • 1





    What's a problem exactly, what don't work?

    – slesh
    Jan 1 at 9:10











  • i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

    – Noob_247
    Jan 1 at 9:16













  • what are you planning to do with the duplicate?

    – GMalc
    Jan 1 at 21:17








1




1





What's a problem exactly, what don't work?

– slesh
Jan 1 at 9:10





What's a problem exactly, what don't work?

– slesh
Jan 1 at 9:10













i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

– Noob_247
Jan 1 at 9:16







i have data as follows 8856 8867 8876 8856 8898 and i want to compare the data with each other if they are same then run a if loop

– Noob_247
Jan 1 at 9:16















what are you planning to do with the duplicate?

– GMalc
Jan 1 at 21:17





what are you planning to do with the duplicate?

– GMalc
Jan 1 at 21:17












3 Answers
3






active

oldest

votes


















0














Just assumption, I see that C2TotalRows=C2TotalRows = Application.CountA(Range("A:A")), which is equal to 1, because of range "A:A". So, For C2row = 2 To C2TotalRows this loop will never run. Try specify another cells range.






share|improve this answer































    0














    It seems that you would like to know how often a CustID (like 8856) occurs in the data and at which row number.
    I created a simple class cInfo for that and then I put the information together in a dictionary. At the end I just printed the information but you could add the code you would like to run



    Here is the class cInfo



    Option Explicit

    Public rowNr As String
    Public ocur As Long


    And that's the code to collect the information



    Sub UniqueValues()
    Dim dict As Scripting.Dictionary
    Dim rg As Range, sngCell As Range
    Dim i As Long
    Dim lRow As Long
    Dim cellInfo As cInfo

    lRow = Range("A1").End(xlDown).Row 'Assumption now free rows and at least on entry in row 2
    Set rg = Range("A2:A" & lRow)
    Set dict = New Dictionary

    For Each sngCell In rg
    If dict.Exists(sngCell.Value) Then
    dict.Item(sngCell.Value).ocur = dict.Item(sngCell.Value).ocur + 1
    dict.Item(sngCell.Value).rowNr = dict.Item(sngCell.Value).rowNr & ";" & CStr(sngCell.Row)
    Else
    Set cellInfo = New cInfo
    cellInfo.rowNr = CStr(sngCell.Row)
    cellInfo.ocur = 1
    dict.Add sngCell.Value, cellInfo
    End If
    Next

    ' Do sth here. I will print some info
    For i = 0 To dict.Count - 1
    Debug.Print "CustID:", dict.Keys(i), dict.Items(i).ocur, "occurence(s) in rows", dict.Items(i).rowNr
    Next

    End Sub


    This worked fine with the example data you provided



    enter image description here



    Output



    enter image description here






    share|improve this answer































      0














      Try this modified your code:



      Private Sub CommandButton2_Click()
      Dim sht1 As Worksheet
      Dim C1row As Long
      Dim CustID As String
      Dim R As Range

      Set sht1 = Worksheets("Report")
      sht1.Activate
      C1row = 2

      Do While sht1.Cells(C1row, 3).Value <> ""
      CustID = sht1.Cells(C1row, 3).Value
      Set R = sht1.Range("C:C").Find(CustID, sht1.Cells(C1row, 3))
      If R.Row > C1row Then
      MsgBox CustID
      End If
      C1row = C1row + 1
      Loop
      End Sub


      Good luck and thank you.



      Reference: Range.Find method (Excel)






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994226%2fcompare-value-of-cells-of-a-column-in-vba%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Just assumption, I see that C2TotalRows=C2TotalRows = Application.CountA(Range("A:A")), which is equal to 1, because of range "A:A". So, For C2row = 2 To C2TotalRows this loop will never run. Try specify another cells range.






        share|improve this answer




























          0














          Just assumption, I see that C2TotalRows=C2TotalRows = Application.CountA(Range("A:A")), which is equal to 1, because of range "A:A". So, For C2row = 2 To C2TotalRows this loop will never run. Try specify another cells range.






          share|improve this answer


























            0












            0








            0







            Just assumption, I see that C2TotalRows=C2TotalRows = Application.CountA(Range("A:A")), which is equal to 1, because of range "A:A". So, For C2row = 2 To C2TotalRows this loop will never run. Try specify another cells range.






            share|improve this answer













            Just assumption, I see that C2TotalRows=C2TotalRows = Application.CountA(Range("A:A")), which is equal to 1, because of range "A:A". So, For C2row = 2 To C2TotalRows this loop will never run. Try specify another cells range.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 9:34









            sleshslesh

            574315




            574315

























                0














                It seems that you would like to know how often a CustID (like 8856) occurs in the data and at which row number.
                I created a simple class cInfo for that and then I put the information together in a dictionary. At the end I just printed the information but you could add the code you would like to run



                Here is the class cInfo



                Option Explicit

                Public rowNr As String
                Public ocur As Long


                And that's the code to collect the information



                Sub UniqueValues()
                Dim dict As Scripting.Dictionary
                Dim rg As Range, sngCell As Range
                Dim i As Long
                Dim lRow As Long
                Dim cellInfo As cInfo

                lRow = Range("A1").End(xlDown).Row 'Assumption now free rows and at least on entry in row 2
                Set rg = Range("A2:A" & lRow)
                Set dict = New Dictionary

                For Each sngCell In rg
                If dict.Exists(sngCell.Value) Then
                dict.Item(sngCell.Value).ocur = dict.Item(sngCell.Value).ocur + 1
                dict.Item(sngCell.Value).rowNr = dict.Item(sngCell.Value).rowNr & ";" & CStr(sngCell.Row)
                Else
                Set cellInfo = New cInfo
                cellInfo.rowNr = CStr(sngCell.Row)
                cellInfo.ocur = 1
                dict.Add sngCell.Value, cellInfo
                End If
                Next

                ' Do sth here. I will print some info
                For i = 0 To dict.Count - 1
                Debug.Print "CustID:", dict.Keys(i), dict.Items(i).ocur, "occurence(s) in rows", dict.Items(i).rowNr
                Next

                End Sub


                This worked fine with the example data you provided



                enter image description here



                Output



                enter image description here






                share|improve this answer




























                  0














                  It seems that you would like to know how often a CustID (like 8856) occurs in the data and at which row number.
                  I created a simple class cInfo for that and then I put the information together in a dictionary. At the end I just printed the information but you could add the code you would like to run



                  Here is the class cInfo



                  Option Explicit

                  Public rowNr As String
                  Public ocur As Long


                  And that's the code to collect the information



                  Sub UniqueValues()
                  Dim dict As Scripting.Dictionary
                  Dim rg As Range, sngCell As Range
                  Dim i As Long
                  Dim lRow As Long
                  Dim cellInfo As cInfo

                  lRow = Range("A1").End(xlDown).Row 'Assumption now free rows and at least on entry in row 2
                  Set rg = Range("A2:A" & lRow)
                  Set dict = New Dictionary

                  For Each sngCell In rg
                  If dict.Exists(sngCell.Value) Then
                  dict.Item(sngCell.Value).ocur = dict.Item(sngCell.Value).ocur + 1
                  dict.Item(sngCell.Value).rowNr = dict.Item(sngCell.Value).rowNr & ";" & CStr(sngCell.Row)
                  Else
                  Set cellInfo = New cInfo
                  cellInfo.rowNr = CStr(sngCell.Row)
                  cellInfo.ocur = 1
                  dict.Add sngCell.Value, cellInfo
                  End If
                  Next

                  ' Do sth here. I will print some info
                  For i = 0 To dict.Count - 1
                  Debug.Print "CustID:", dict.Keys(i), dict.Items(i).ocur, "occurence(s) in rows", dict.Items(i).rowNr
                  Next

                  End Sub


                  This worked fine with the example data you provided



                  enter image description here



                  Output



                  enter image description here






                  share|improve this answer


























                    0












                    0








                    0







                    It seems that you would like to know how often a CustID (like 8856) occurs in the data and at which row number.
                    I created a simple class cInfo for that and then I put the information together in a dictionary. At the end I just printed the information but you could add the code you would like to run



                    Here is the class cInfo



                    Option Explicit

                    Public rowNr As String
                    Public ocur As Long


                    And that's the code to collect the information



                    Sub UniqueValues()
                    Dim dict As Scripting.Dictionary
                    Dim rg As Range, sngCell As Range
                    Dim i As Long
                    Dim lRow As Long
                    Dim cellInfo As cInfo

                    lRow = Range("A1").End(xlDown).Row 'Assumption now free rows and at least on entry in row 2
                    Set rg = Range("A2:A" & lRow)
                    Set dict = New Dictionary

                    For Each sngCell In rg
                    If dict.Exists(sngCell.Value) Then
                    dict.Item(sngCell.Value).ocur = dict.Item(sngCell.Value).ocur + 1
                    dict.Item(sngCell.Value).rowNr = dict.Item(sngCell.Value).rowNr & ";" & CStr(sngCell.Row)
                    Else
                    Set cellInfo = New cInfo
                    cellInfo.rowNr = CStr(sngCell.Row)
                    cellInfo.ocur = 1
                    dict.Add sngCell.Value, cellInfo
                    End If
                    Next

                    ' Do sth here. I will print some info
                    For i = 0 To dict.Count - 1
                    Debug.Print "CustID:", dict.Keys(i), dict.Items(i).ocur, "occurence(s) in rows", dict.Items(i).rowNr
                    Next

                    End Sub


                    This worked fine with the example data you provided



                    enter image description here



                    Output



                    enter image description here






                    share|improve this answer













                    It seems that you would like to know how often a CustID (like 8856) occurs in the data and at which row number.
                    I created a simple class cInfo for that and then I put the information together in a dictionary. At the end I just printed the information but you could add the code you would like to run



                    Here is the class cInfo



                    Option Explicit

                    Public rowNr As String
                    Public ocur As Long


                    And that's the code to collect the information



                    Sub UniqueValues()
                    Dim dict As Scripting.Dictionary
                    Dim rg As Range, sngCell As Range
                    Dim i As Long
                    Dim lRow As Long
                    Dim cellInfo As cInfo

                    lRow = Range("A1").End(xlDown).Row 'Assumption now free rows and at least on entry in row 2
                    Set rg = Range("A2:A" & lRow)
                    Set dict = New Dictionary

                    For Each sngCell In rg
                    If dict.Exists(sngCell.Value) Then
                    dict.Item(sngCell.Value).ocur = dict.Item(sngCell.Value).ocur + 1
                    dict.Item(sngCell.Value).rowNr = dict.Item(sngCell.Value).rowNr & ";" & CStr(sngCell.Row)
                    Else
                    Set cellInfo = New cInfo
                    cellInfo.rowNr = CStr(sngCell.Row)
                    cellInfo.ocur = 1
                    dict.Add sngCell.Value, cellInfo
                    End If
                    Next

                    ' Do sth here. I will print some info
                    For i = 0 To dict.Count - 1
                    Debug.Print "CustID:", dict.Keys(i), dict.Items(i).ocur, "occurence(s) in rows", dict.Items(i).rowNr
                    Next

                    End Sub


                    This worked fine with the example data you provided



                    enter image description here



                    Output



                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 10:09









                    StoraxStorax

                    4,2183518




                    4,2183518























                        0














                        Try this modified your code:



                        Private Sub CommandButton2_Click()
                        Dim sht1 As Worksheet
                        Dim C1row As Long
                        Dim CustID As String
                        Dim R As Range

                        Set sht1 = Worksheets("Report")
                        sht1.Activate
                        C1row = 2

                        Do While sht1.Cells(C1row, 3).Value <> ""
                        CustID = sht1.Cells(C1row, 3).Value
                        Set R = sht1.Range("C:C").Find(CustID, sht1.Cells(C1row, 3))
                        If R.Row > C1row Then
                        MsgBox CustID
                        End If
                        C1row = C1row + 1
                        Loop
                        End Sub


                        Good luck and thank you.



                        Reference: Range.Find method (Excel)






                        share|improve this answer




























                          0














                          Try this modified your code:



                          Private Sub CommandButton2_Click()
                          Dim sht1 As Worksheet
                          Dim C1row As Long
                          Dim CustID As String
                          Dim R As Range

                          Set sht1 = Worksheets("Report")
                          sht1.Activate
                          C1row = 2

                          Do While sht1.Cells(C1row, 3).Value <> ""
                          CustID = sht1.Cells(C1row, 3).Value
                          Set R = sht1.Range("C:C").Find(CustID, sht1.Cells(C1row, 3))
                          If R.Row > C1row Then
                          MsgBox CustID
                          End If
                          C1row = C1row + 1
                          Loop
                          End Sub


                          Good luck and thank you.



                          Reference: Range.Find method (Excel)






                          share|improve this answer


























                            0












                            0








                            0







                            Try this modified your code:



                            Private Sub CommandButton2_Click()
                            Dim sht1 As Worksheet
                            Dim C1row As Long
                            Dim CustID As String
                            Dim R As Range

                            Set sht1 = Worksheets("Report")
                            sht1.Activate
                            C1row = 2

                            Do While sht1.Cells(C1row, 3).Value <> ""
                            CustID = sht1.Cells(C1row, 3).Value
                            Set R = sht1.Range("C:C").Find(CustID, sht1.Cells(C1row, 3))
                            If R.Row > C1row Then
                            MsgBox CustID
                            End If
                            C1row = C1row + 1
                            Loop
                            End Sub


                            Good luck and thank you.



                            Reference: Range.Find method (Excel)






                            share|improve this answer













                            Try this modified your code:



                            Private Sub CommandButton2_Click()
                            Dim sht1 As Worksheet
                            Dim C1row As Long
                            Dim CustID As String
                            Dim R As Range

                            Set sht1 = Worksheets("Report")
                            sht1.Activate
                            C1row = 2

                            Do While sht1.Cells(C1row, 3).Value <> ""
                            CustID = sht1.Cells(C1row, 3).Value
                            Set R = sht1.Range("C:C").Find(CustID, sht1.Cells(C1row, 3))
                            If R.Row > C1row Then
                            MsgBox CustID
                            End If
                            C1row = C1row + 1
                            Loop
                            End Sub


                            Good luck and thank you.



                            Reference: Range.Find method (Excel)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 5:46









                            onorioonorio

                            262




                            262






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994226%2fcompare-value-of-cells-of-a-column-in-vba%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas

                                Can't read property showImagePicker of undefined in react native iOS