+ Reply to Thread
Results 1 to 12 of 12

Code to count under certain condition

  1. #1
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Code to count under certain condition

    Hi everyone,
    Could someone identify whatīs wrong with my code below?
    I want it to count under a certain condition and then store the total number in a variable, but Iīm getting a "0" answer. The code is like follows:

    Please Login or Register  to view this content.
    Tnx!
    Last edited by Marceltcm; 08-29-2013 at 03:13 PM.

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Whatīs wrong with my code

    You must test each cell separately.
    Gary's Student

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Whatīs wrong with my code

    Hi, Marceltcm,

    please wrap up your procedure in code-tags.

    I wouldnīt use a loop:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Annd maybe you could alter the title for this thread - itīs not really what Forum Rule #1 calls for.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Whatīs wrong with my code

    Quote Originally Posted by Jakobshavn View Post
    You must test each cell separately.
    I tried it but didnīt work yet

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,844

    Re: Whatīs wrong with my code

    Try:
    Please Login or Register  to view this content.

  6. #6
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,453

    Re: Whatīs wrong with my code

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  7. #7
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Whatīs wrong with my code

    Quote Originally Posted by HaHoBe View Post
    Hi, Marceltcm,

    please wrap up your procedure in code-tags.

    I wouldnīt use a loop:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Annd maybe you could alter the title for this thread - itīs not really what Forum Rule #1 calls for.

    Ciao,
    Holger
    I couldnīt do in that way because I have to test the three numbers of each line, if they satisfy the condition then I sum 1 to the variable

  8. #8
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Whatīs wrong with my code

    Quote Originally Posted by Pepe Le Mokko View Post
    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.


    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Sorry, Iīve changed it already.
    Tnx

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code to count under certain condition

    Hi, Marceltcm,

    as you have changed the title maybe you also add the code-tags in the opening post.

    Sorry for my fault (I used a less instead of a greater sign). Shouldnīt it be And instead of Or for summing up each single line?
    Please Login or Register  to view this content.
    And please do not quote full posts but only the necessary parts of these posts if neede (or make clear to which person you are answering).

    Ciao,
    Holher

  10. #10
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Code to count under certain condition

    Tnx HaHoBe! I think it worked now. But i wish to understand why the dots before "cells".
    Tnkx!

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Code to count under certain condition

    Hi, Marceltcm,

    in your code you activated Sheets("Geral"), so it was the ActiveSheet. Any Ranges and Cells are taken from the active sheet.

    I used a With-Statement which doesnīt make it necessary that Sheet("Geral") is the active sheet. But having stated above that any range or cell refers to the active sheet I must make clear to VBA to take the ranges and cells from the sheet which is referred to in the With-Statement (thus using the dot in front of Cells(...)).

    You still havenīt added code-tags in the opening post.

    Ciao,
    Holger

  12. #12
    Registered User
    Join Date
    08-29-2013
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Code to count under certain condition

    Got it, tnx so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. what is wrong with this code- error from vba code
    By punna111 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2013, 03:14 PM
  2. Code for email alerts from excel isn't working, wrong code possibly?
    By jessthorogood in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 01:45 AM
  3. Wrong line of code banned me out and can't access vba code...
    By albertc in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-14-2012, 02:41 PM
  4. what is wrong with the code
    By blyzzard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2011, 02:56 AM
  5. [SOLVED] What's wrong with my code ?
    By christophe meresse in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM

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