Results 1 to 9 of 9

Thread: [RESOLVED] Counting non-blank cells in constantly changing range

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Resolved [RESOLVED] Counting non-blank cells in constantly changing range

    I've been using the following code to count the number of employees that participated in a particular job task:

    NumOfWorkers = Application.CountA(Sheets("Paycard").Range("F3:K3")

    I need to know if it's possible with this function to now set the "Range" portion of it to a constantly changing set of variables? What is the syntax?

    For example, as I adjust the variable 'currentrow' plus one after each loop, I need to know the NEW number of workers for that particular row, not just row 3.

    Make sense? Help?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Counting non-blank cells in constantly changing range

    try like
    vb Code:
    1. NumOfWorkers = Application.CountA(Sheets("Paycard").Range("F3:K" & rw)
    where rw is your row variable
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Re: Counting non-blank cells in constantly changing range

    Quote Originally Posted by westconn1 View Post
    try like
    vb Code:
    1. NumOfWorkers = Application.CountA(Sheets("Paycard").Range("F3:K" & rw)
    where rw is your row variable
    Definitely appreciate the input, but that route doesn't get the job done.

    .Range("F" & rw:"K" & rw) returns a compile error.

    As does a whole set of combinations like:

    .Range(""F" & rw: "K" & rw"")


    I need to be able to change the row variable for both sides of the range.

  4. #4
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Counting non-blank cells in constantly changing range

    there are anumber of ways to attack this problem, but they all need a little more information than you have given...

    for a start why do you curtail the row to just this range what is outwith the range?

    why are you looping through the rows or have i missed somethig here?

    i think i got the idea that you want to do a block from f to k fro row 1 to 6 for example?

    here to help

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Re: Counting non-blank cells in constantly changing range

    Quote Originally Posted by incidentals View Post
    there are anumber of ways to attack this problem, but they all need a little more information than you have given...

    for a start why do you curtail the row to just this range what is outwith the range?

    why are you looping through the rows or have i missed somethig here?

    i think i got the idea that you want to do a block from f to k fro row 1 to 6 for example?

    here to help
    You got it. I have a worksheet where the crew leader for the day adds the list of jobs that crew did, one to each row, then lists the crew members that participated in that particular job horizontally, starting in column F and moving right. There could be anywhere from 1 to 8 or 9 crew members on a particular job.

    Instead of having the crew leader type in manually how many people were on the jobsite, THEN list them all out, I simply wanted to calculate how many people were on a particular jobsite based on how many names were entered in from Column F on through Column K (for the current row) or however many I need to contain all the names.

    After calculating pay for the current row, I add one to the row counter and loop through again, adding up each crew member's pay for the whole week for all jobs put together.

    Our crew members are paid based on a percentage of the overall job amount; that percentage changes depending on how many people were working on the job.

    You were on the right track, yes.

  6. #6
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Counting non-blank cells in constantly changing range

    the loop man not be necessary!
    there are many ways to skin this particular cat!
    what are the the preceeding columns a to e?

    i ask because you may be able to filter the row in some way to get a range-less answer back...

    here to talk

    just so you kow its worthwhile talking ...

    suggestion 1 ( programming aside ) a new column e is introduced and returns the countif() of the rest of the row.
    suggestion 2 ( programming aside ) the rest of the row is a named range - this is because names ranges expand when new columns are inserted - requires that the users insert new columns rather than just put more data in the next available column so its prone to disaster, but is used in some situations!

    just the first 2 thoughts
    Last edited by incidentals; Feb 14th, 2012 at 02:01 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Re: Counting non-blank cells in constantly changing range

    Quote Originally Posted by incidentals View Post

    suggestion 1 ( programming aside ) a new column e is introduced and returns the countif() of the rest of the row.
    This is the route I ended up taking. It's not as technically clean as I usually like to do things, but I simply picked the next unused column, filled it down with a "countA" formula, then set the text in that column to invisible and locked it for editing.

    The macro knows it's there and uses the employee count from that column, but the end user (i.e. my employees) don't and never see it.

    I'm convinced (without any real justification) that there is a way to do this programming-wise, but hell....if it works, it works.

    I'll sleep at night, even though I know it's there.

    Thanks for the suggestions.

  8. #8
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: [RESOLVED] Counting non-blank cells in constantly changing range

    please rate the post if it helped and close the thread using the toolbar at the top

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Counting non-blank cells in constantly changing range

    As does a whole set of combinations like:

    .Range(""F" & rw: "K" & rw"")
    try
    vb Code:
    1. .range("f" & rw & ":K" & rw)
    or use cells(rw, col) for start & end of range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width