Count occurences of a value in a column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bexxx
    New Member
    • Jul 2007
    • 3

    Count occurences of a value in a column

    I need to know how to count the number of "1s" in a column and then input the total number of "1s" in that column on the screen.
    Hence counting the total of these numbers in this column.
    PLEASE HELP ME!
    I will be forever greatful to you if you do!!
  • fplesco
    New Member
    • Jul 2007
    • 82

    #2
    Originally posted by bexxx
    I need to know how to count the number of "1s" in a column and then input the total number of "1s" in that column on the screen.
    Hence counting the total of these numbers in this column.
    PLEASE HELP ME!
    I will be forever greatful to you if you do!!
    Hi -

    This is quite like a high level perspective of your problem but as I see it.

    Code:
    For Row = 1 to TotalRows 
          intColTotalOne = 0
          For Cols = 1 to TotalCols
                if <Grid Object>.Textmatrix(Row,Cols) = "1" then 
                   'OR CINT(<GridObject>.Textmatrix(Row,Cols)) = 1
                    intColTotalOne = intColTotalOne + 1
                end if
         Next Row
    
         Print intColTotalOne
    Next Row

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by bexxx
      I need to know how to count the number of "1s" in a column and then input the total number of "1s" in that column on the screen.
      Hence counting the total of these numbers in this column.
      You might want to check your wording, for a start. If you input the number (that is, accept it from the user via keyboard or whatever), then you don't need to calculate it.

      Can you give us some more details about the situation? For instance, what version of VB are you using? What kind of "column" are you talking about? Something in Excel or Access, perhaps?

      We're not mind-readers.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by fplesco
        ... For Row = 1 to TotalRows ...
        Thanks for taking a shot at it, fplesco. However, I would question your logic. It looks to me as though you are tallying up and displaying the "1"s per row, not those in a particular column.

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by Killer42
          Thanks for taking a shot at it, fplesco. However, I would question your logic. It looks to me as though you are tallying up and displaying the "1"s per row, not those in a particular column.
          yes you are correct Killer, and need change the for loop's Rows to Column and column into Rows or with help of arraylist he has to print.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by hariharanmca
            yes you are correct Killer, and need change the for loop's Rows to Column and column into Rows or with help of arraylist he has to print.
            If we assume that the OP is using some sort of grid which allows direct access to each cell, as in the posted code, then a requirement to work with a particular column doesn't need nested loops at all. Just one loop, to go through the rows.

            Comment

            • fplesco
              New Member
              • Jul 2007
              • 82

              #7
              Originally posted by Killer42
              Thanks for taking a shot at it, fplesco. However, I would question your logic. It looks to me as though you are tallying up and displaying the "1"s per row, not those in a particular column.

              I got it wrong. I agree with you guys.

              Thanks

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by Killer42
                If we assume that the OP is using some sort of grid which allows direct access to each cell, as in the posted code, then a requirement to work with a particular column doesn't need nested loops at all. Just one loop, to go through the rows.
                i cannot get what you mean
                Originally posted by Killer42
                which allows direct access to each cell.

                If he wants for a single column count. yha it's possible in one 'for'. But if it is contain multipule colume and rows, we have to use 2 'for' loop?

                Comment

                • bexxx
                  New Member
                  • Jul 2007
                  • 3

                  #9
                  Ok so ive thought about it and what i really need is a simple count function to go down the column and count the "1"s. Here ill give you an example:
                  This is what VB prints on the screen (it is information retreived from a Microsoft Excel File)

                  1 3 4
                  2 5 1
                  4 3 2
                  4 3 1
                  1 4 3
                  3 3 5

                  So i want the code to be able to go:
                  ok in column 1:
                  1 = 2
                  2 = 1
                  3 = 1
                  4 = 2
                  Etc. I want it to do this for all three columns and print the values on screen.
                  Does anyone know how to do this???

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by hariharanmca
                    If he wants for a single column count. yha it's possible in one 'for'. But if it is contain multipule colume and rows, we have to use 2 'for' loop?
                    How many rows or columns are contained in a grid is irrelevant. You only need the nested loops if you want to process multiple columns in multiple rows. That was not what the original question stated. To process a single column, you just need to loop through the rows, accessing the same column each time around the loop.

                    Anyway, I think this is one of those cases where we're both just saying the same thing, different ways.

                    Comment

                    Working...