Check values in column(s) if all are the same











up vote
2
down vote

favorite












Currently I'm creating a check for a column.



Goal: I have a column called currency which I need to check if they are all the same for each Bank (Column A). If there are other currency then it will prompt me.



Additional goal: I would also like to include in the checking the one in column E (Currency (Bank Charge)) to make sure that all currencies for that bank are the same.



Problem: I already have a working code using scripting.dictionary, however, I have some trouble clearing the dictionary for the first loop / currencies for the first Bank. I tried to clear the dictionary before it proceeds to another bank. But it is not working.



Below is the screenshot of what I would like to check:



enter image description here



Below is the current code that I have:



Sub CurrencyTestCheck()

Dim wksSource As Worksheet: Set wksSource = ThisWorkbook.Sheets("Test1")

Dim i As Long
Dim x As Long
Dim lastRow As Long
Dim strBankName As String

Set d = CreateObject("Scripting.dictionary")

Application.ScreenUpdating = False

lastRow = wksSource.Cells(wksSource.Rows.Count, "C").End(xlUp).Row

For i = 2 To lastRow

If Len(wksSource.Cells(i, 1).Value) > 0 Then 'If a new bank starts

If Len(strBankName) > 0 Then

For Each k In d.Keys

strCheck = k
countCurrency = d(k)

msg = msg & strCheck & " - " & countCurrency & vbNewLine
x = x + 1

Next k

If x > 1 Then

MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
vbNewLine & msg, vbCritical, "Warning"

Else

MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

End If

d.RemoveAll

End If


strBankName = wksSource.Cells(i, 1).Value

End If

'Currency for each Bank

tmp = Trim(wksSource.Cells(i, 3).Value)
If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

Next i

If Len(strBankName) > 0 Then

For Each k In d.Keys

strCheck = k
countCurrency = d(k)

msg = msg & strCheck & " - " & countCurrency & vbNewLine
x = x + 1

Next k

If x > 1 Then

MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
vbNewLine & msg, vbCritical, "Warning"

Else

MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

End If

End If

Application.ScreenUpdating = True

End Sub


Output:



enter image description here



enter image description here



Previous values are still in the dictionary (USD - 3 and AUD - 2)



Appreciate if you also have another suggestion to do the checking.










share|improve this question


























    up vote
    2
    down vote

    favorite












    Currently I'm creating a check for a column.



    Goal: I have a column called currency which I need to check if they are all the same for each Bank (Column A). If there are other currency then it will prompt me.



    Additional goal: I would also like to include in the checking the one in column E (Currency (Bank Charge)) to make sure that all currencies for that bank are the same.



    Problem: I already have a working code using scripting.dictionary, however, I have some trouble clearing the dictionary for the first loop / currencies for the first Bank. I tried to clear the dictionary before it proceeds to another bank. But it is not working.



    Below is the screenshot of what I would like to check:



    enter image description here



    Below is the current code that I have:



    Sub CurrencyTestCheck()

    Dim wksSource As Worksheet: Set wksSource = ThisWorkbook.Sheets("Test1")

    Dim i As Long
    Dim x As Long
    Dim lastRow As Long
    Dim strBankName As String

    Set d = CreateObject("Scripting.dictionary")

    Application.ScreenUpdating = False

    lastRow = wksSource.Cells(wksSource.Rows.Count, "C").End(xlUp).Row

    For i = 2 To lastRow

    If Len(wksSource.Cells(i, 1).Value) > 0 Then 'If a new bank starts

    If Len(strBankName) > 0 Then

    For Each k In d.Keys

    strCheck = k
    countCurrency = d(k)

    msg = msg & strCheck & " - " & countCurrency & vbNewLine
    x = x + 1

    Next k

    If x > 1 Then

    MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
    vbNewLine & msg, vbCritical, "Warning"

    Else

    MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

    End If

    d.RemoveAll

    End If


    strBankName = wksSource.Cells(i, 1).Value

    End If

    'Currency for each Bank

    tmp = Trim(wksSource.Cells(i, 3).Value)
    If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

    Next i

    If Len(strBankName) > 0 Then

    For Each k In d.Keys

    strCheck = k
    countCurrency = d(k)

    msg = msg & strCheck & " - " & countCurrency & vbNewLine
    x = x + 1

    Next k

    If x > 1 Then

    MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
    vbNewLine & msg, vbCritical, "Warning"

    Else

    MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

    End If

    End If

    Application.ScreenUpdating = True

    End Sub


    Output:



    enter image description here



    enter image description here



    Previous values are still in the dictionary (USD - 3 and AUD - 2)



    Appreciate if you also have another suggestion to do the checking.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Currently I'm creating a check for a column.



      Goal: I have a column called currency which I need to check if they are all the same for each Bank (Column A). If there are other currency then it will prompt me.



      Additional goal: I would also like to include in the checking the one in column E (Currency (Bank Charge)) to make sure that all currencies for that bank are the same.



      Problem: I already have a working code using scripting.dictionary, however, I have some trouble clearing the dictionary for the first loop / currencies for the first Bank. I tried to clear the dictionary before it proceeds to another bank. But it is not working.



      Below is the screenshot of what I would like to check:



      enter image description here



      Below is the current code that I have:



      Sub CurrencyTestCheck()

      Dim wksSource As Worksheet: Set wksSource = ThisWorkbook.Sheets("Test1")

      Dim i As Long
      Dim x As Long
      Dim lastRow As Long
      Dim strBankName As String

      Set d = CreateObject("Scripting.dictionary")

      Application.ScreenUpdating = False

      lastRow = wksSource.Cells(wksSource.Rows.Count, "C").End(xlUp).Row

      For i = 2 To lastRow

      If Len(wksSource.Cells(i, 1).Value) > 0 Then 'If a new bank starts

      If Len(strBankName) > 0 Then

      For Each k In d.Keys

      strCheck = k
      countCurrency = d(k)

      msg = msg & strCheck & " - " & countCurrency & vbNewLine
      x = x + 1

      Next k

      If x > 1 Then

      MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
      vbNewLine & msg, vbCritical, "Warning"

      Else

      MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

      End If

      d.RemoveAll

      End If


      strBankName = wksSource.Cells(i, 1).Value

      End If

      'Currency for each Bank

      tmp = Trim(wksSource.Cells(i, 3).Value)
      If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

      Next i

      If Len(strBankName) > 0 Then

      For Each k In d.Keys

      strCheck = k
      countCurrency = d(k)

      msg = msg & strCheck & " - " & countCurrency & vbNewLine
      x = x + 1

      Next k

      If x > 1 Then

      MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
      vbNewLine & msg, vbCritical, "Warning"

      Else

      MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

      End If

      End If

      Application.ScreenUpdating = True

      End Sub


      Output:



      enter image description here



      enter image description here



      Previous values are still in the dictionary (USD - 3 and AUD - 2)



      Appreciate if you also have another suggestion to do the checking.










      share|improve this question













      Currently I'm creating a check for a column.



      Goal: I have a column called currency which I need to check if they are all the same for each Bank (Column A). If there are other currency then it will prompt me.



      Additional goal: I would also like to include in the checking the one in column E (Currency (Bank Charge)) to make sure that all currencies for that bank are the same.



      Problem: I already have a working code using scripting.dictionary, however, I have some trouble clearing the dictionary for the first loop / currencies for the first Bank. I tried to clear the dictionary before it proceeds to another bank. But it is not working.



      Below is the screenshot of what I would like to check:



      enter image description here



      Below is the current code that I have:



      Sub CurrencyTestCheck()

      Dim wksSource As Worksheet: Set wksSource = ThisWorkbook.Sheets("Test1")

      Dim i As Long
      Dim x As Long
      Dim lastRow As Long
      Dim strBankName As String

      Set d = CreateObject("Scripting.dictionary")

      Application.ScreenUpdating = False

      lastRow = wksSource.Cells(wksSource.Rows.Count, "C").End(xlUp).Row

      For i = 2 To lastRow

      If Len(wksSource.Cells(i, 1).Value) > 0 Then 'If a new bank starts

      If Len(strBankName) > 0 Then

      For Each k In d.Keys

      strCheck = k
      countCurrency = d(k)

      msg = msg & strCheck & " - " & countCurrency & vbNewLine
      x = x + 1

      Next k

      If x > 1 Then

      MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
      vbNewLine & msg, vbCritical, "Warning"

      Else

      MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

      End If

      d.RemoveAll

      End If


      strBankName = wksSource.Cells(i, 1).Value

      End If

      'Currency for each Bank

      tmp = Trim(wksSource.Cells(i, 3).Value)
      If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

      Next i

      If Len(strBankName) > 0 Then

      For Each k In d.Keys

      strCheck = k
      countCurrency = d(k)

      msg = msg & strCheck & " - " & countCurrency & vbNewLine
      x = x + 1

      Next k

      If x > 1 Then

      MsgBox "There are different currencies for bank " & strBankName & vbNewLine & _
      vbNewLine & msg, vbCritical, "Warning"

      Else

      MsgBox "Currencies are all the same for " & strBankName, vbInformation, "Same currencies"

      End If

      End If

      Application.ScreenUpdating = True

      End Sub


      Output:



      enter image description here



      enter image description here



      Previous values are still in the dictionary (USD - 3 and AUD - 2)



      Appreciate if you also have another suggestion to do the checking.







      excel vba excel-vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 6:08









      Sevpoint

      80110




      80110
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You might have forgotten to reset your currency discrepancy counter x.

          Set it to x = 0 after the first bank's loop.



          i.e.



          ...
          ...

          'Currency for each Bank

          tmp = Trim(wksSource.Cells(i, 3).Value)
          If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

          Next i

          ' Add these two lines:
          x = 0
          msg = ""

          If Len(strBankName) > 0 Then

          For Each k In d.Keys

          strCheck = k

          ...
          ...


          And like TinMan said, also reset the msg so the previous bank's results don't leak into your the next bank.






          share|improve this answer





















          • I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
            – libzz
            Nov 20 at 6:42












          • Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
            – Sevpoint
            Nov 20 at 6:57











          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%2f53387170%2fcheck-values-in-columns-if-all-are-the-same%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You might have forgotten to reset your currency discrepancy counter x.

          Set it to x = 0 after the first bank's loop.



          i.e.



          ...
          ...

          'Currency for each Bank

          tmp = Trim(wksSource.Cells(i, 3).Value)
          If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

          Next i

          ' Add these two lines:
          x = 0
          msg = ""

          If Len(strBankName) > 0 Then

          For Each k In d.Keys

          strCheck = k

          ...
          ...


          And like TinMan said, also reset the msg so the previous bank's results don't leak into your the next bank.






          share|improve this answer





















          • I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
            – libzz
            Nov 20 at 6:42












          • Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
            – Sevpoint
            Nov 20 at 6:57















          up vote
          1
          down vote



          accepted










          You might have forgotten to reset your currency discrepancy counter x.

          Set it to x = 0 after the first bank's loop.



          i.e.



          ...
          ...

          'Currency for each Bank

          tmp = Trim(wksSource.Cells(i, 3).Value)
          If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

          Next i

          ' Add these two lines:
          x = 0
          msg = ""

          If Len(strBankName) > 0 Then

          For Each k In d.Keys

          strCheck = k

          ...
          ...


          And like TinMan said, also reset the msg so the previous bank's results don't leak into your the next bank.






          share|improve this answer





















          • I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
            – libzz
            Nov 20 at 6:42












          • Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
            – Sevpoint
            Nov 20 at 6:57













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You might have forgotten to reset your currency discrepancy counter x.

          Set it to x = 0 after the first bank's loop.



          i.e.



          ...
          ...

          'Currency for each Bank

          tmp = Trim(wksSource.Cells(i, 3).Value)
          If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

          Next i

          ' Add these two lines:
          x = 0
          msg = ""

          If Len(strBankName) > 0 Then

          For Each k In d.Keys

          strCheck = k

          ...
          ...


          And like TinMan said, also reset the msg so the previous bank's results don't leak into your the next bank.






          share|improve this answer












          You might have forgotten to reset your currency discrepancy counter x.

          Set it to x = 0 after the first bank's loop.



          i.e.



          ...
          ...

          'Currency for each Bank

          tmp = Trim(wksSource.Cells(i, 3).Value)
          If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1

          Next i

          ' Add these two lines:
          x = 0
          msg = ""

          If Len(strBankName) > 0 Then

          For Each k In d.Keys

          strCheck = k

          ...
          ...


          And like TinMan said, also reset the msg so the previous bank's results don't leak into your the next bank.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 6:39









          libzz

          112112




          112112












          • I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
            – libzz
            Nov 20 at 6:42












          • Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
            – Sevpoint
            Nov 20 at 6:57


















          • I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
            – libzz
            Nov 20 at 6:42












          • Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
            – Sevpoint
            Nov 20 at 6:57
















          I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
          – libzz
          Nov 20 at 6:42






          I'm not sure if it's the correct timing but the key point here is resetting your variables before proceeding to looping the next bank.
          – libzz
          Nov 20 at 6:42














          Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
          – Sevpoint
          Nov 20 at 6:57




          Indeed, it's the x that I missed to reset. I've tried to add the suggested answer and it worked. Thanks a lot!
          – Sevpoint
          Nov 20 at 6:57


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53387170%2fcheck-values-in-columns-if-all-are-the-same%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga