+ Reply to Thread
Results 1 to 5 of 5

count the number of cells if condition satisfy

  1. #1
    Registered User
    Join Date
    11-17-2012
    Location
    sri lanka
    MS-Off Ver
    Excel 2010
    Posts
    13

    count the number of cells if condition satisfy

    Hello Everyone,

    currently I am developing a code to count the number of cells if value is same as what i am looking for.

    EX; cell values are as follows

    TGN
    a1 3001
    a2 3001
    a3 3001
    a4 3001
    a5 3001
    a6 2000
    a7 2000
    a8 2000
    a9 2000
    a10 2000

    I want to count the number of cells where TGN = 3001 and number of data changes with time to time as per the time of generating the report.

    finally i want to count the number of cell which contains TGN = 3001 and assign final call position to a to a variable.

    I did something like bellow but it didn't work.

    Dim tgn As Range
    Set tgn = Worksheets("sheet1").Range("a2")


    Dim newCell As Range '<== variable used for final cell position
    Set newCell = Worksheets("sheet1").Range("a2")


    Dim tgn1 As Integer
    tgn1 = 3001

    Dim a As Integer '<== variable used for final count
    a = 0


    Do While (tgn.Value = tgn1)

    tgn = Range("newCell").End(xlDown)
    newCell = Range("newCell").Offset(1, 0)
    a = a + 1

    Loop

    please help me to develop code for this.

    thanks
    Last edited by rosh@excel; 11-20-2012 at 07:14 AM. Reason: insert attachment

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: count the number of cells if condition satisfy

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    07-04-2012
    Location
    Al Khor, Qatar
    MS-Off Ver
    Excel 2013
    Posts
    101

    Re: count the number of cells if condition satisfy

    Can you post a sample workbook. I'll give it a try. please include in the sample workbook that inputs, the complete details and also the real output.

  4. #4
    Registered User
    Join Date
    11-17-2012
    Location
    sri lanka
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: count the number of cells if condition satisfy

    Hi,

    here is the full code which i wrote to generate two line graphs using Column B, C and D which are stored in the range variable x, y, Z.

    but the length of those variables changes with the time i get the report ( graph should only be generated when the column A = 3001, for others it is a different graph which i have not included yet)

    work sheet is attached herewith

    Please Login or Register  to view this content.
    Thanks
    Attached Files Attached Files
    Last edited by rosh@excel; 11-20-2012 at 07:26 AM.

  5. #5
    Registered User
    Join Date
    11-17-2012
    Location
    sri lanka
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: count the number of cells if condition satisfy

    hey i found countif function might be able to use to count the number of cells.

    but i got syntax error, can anyone advice me.

    my intention is to count the number of cells within the range A2:A200 where value equals to 3001

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1