+ Reply to Thread
Results 1 to 40 of 40

Detect first and last row and count blanks in between

  1. #1
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Detect first and last row and count blanks in between

    Hi all, apologies in advance if this has been covered in previous threads but my search came up unsuccessful.

    I have attached an example sheet.
    I basically need to count the blank cells in the range from when the first number at the top starts to the last number.
    So in col1 would be 2 in col2 would be 4.
    Ideally just counting them or as an alternative fill with a value like a “Z”.
    There are other columns on the right and so this will carry on, for example, until column Z.

    Many thanks 😊
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Detect first and last row and count blanks in between

    Hi Musto,

    I think a CountIf() function is what you want. Something like:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    See the attached file below.
    CountIf Blanks in a Range.xlsx

    Hey Musto,

    I got excited about the answer before I reread the problem. The above will count all blanks from B2 to B25. Give me a minute to do your problem now...
    Last edited by MarvinP; 05-11-2022 at 07:37 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Detect first and last row and count blanks in between

    OK Musto,

    This was harder than I wanted it to be: I got:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    But you'd need to change all the "25" to the maximum row number you were checking in the formula above.

    See how I did it in the attached below, using 3 helper cells to wrestle the above formula into a single formula.
    CountIf Blanks in a Range Try 2.xlsx

  4. #4
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    Thanks Marvin but I was looking for something to be executed with a code and to be neater. I believe it is possible with first and last row search and loop until blanks are found, or something similar.

  5. #5
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Detect first and last row and count blanks in between

    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Musto85,

    As I don't know how/where you want the result, so this is to just a demo,
    In order to distinguish when no number and only one number in the column, it will give Error when no number and 0 for only one number.
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    Thanks nimrod1313 you star !

    Can this be looped for all the column from B to Z for example?
    And can the count be shown in a cell instead of the msgbox please?
    Last cell just below the last hour on each column will do.
    Thank you very very much

  8. #8
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    Update: I’ve sorted the count on the cell instead of msgbox.

    Still need looping for the other columns

  9. #9
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Detect first and last row and count blanks in between

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 05-11-2022 at 11:12 PM.

  10. #10
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    That’s wonderful.

    Thank you so much sir!

  11. #11
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Detect first and last row and count blanks in between

    Hi Musto,

    As long as were showing different VBA to do this problem, here is my style. See if it is more understandable. I use a SpecialCells to count those blanks.
    Please Login or Register  to view this content.
    I find it fun to read other's code and learn from them.

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    Another angle of attack.
    Total amount.
    Please Login or Register  to view this content.
    Individual Columns.
    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    No hard coding the row number for the totals or the Columns to work on.
    Please Login or Register  to view this content.
    Last edited by jolivanes; 05-12-2022 at 01:17 AM.

  14. #14
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Thanks MarvinP and jolivanes. That’s fantastic guys, thank you all so much.

    How would I go about if I want those empty cells in the middle to be filled with the average between the number above and below?

    I found something like this but only works for all the empty cells

    Sub Test()
    Dim i As Long, j As Long, n As Long
    Application.ScreenUpdating = False
    n = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To n - 1
    If Cells(i, "A") = "" Then
    j = i
    Do While Cells(i, "A") = ""
    i = i + 1
    Loop
    Range(Cells(j, "A"), Cells(i - 1, "A")) = (Cells(j - 1, "A") + Cells(i, "A")) / 2
    End If
    Next

    Application.ScreenUpdating = True
    End Sub

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Please Login or Register  to view this content.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    Re: "those empty cells in the middle to be filled with the average between the number above and below?"

    So maybe.
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    jolivanes: good try, it did fill with the average the cells in the middle but it also filled all the other cells after the last row.

  18. #18
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    [QUOTE=jindon;5675217]


    jindon I struggle to understand your code but it’s art and works brilliantly!
    Is there a way that can be slightly adjusted to give the total of blanks cell for each column at the bottom instead of the message box?
    Thanks in advance

  19. #19
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    Re Post #17
    Maybe you should look at your workbook at find out where your last used cell is.

  20. #20
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    Did you change anything in the macros?
    When you say "filled all the other cells after the last row", what do you mean by that? Do the cells at the last row level have that total amount of empties in it?
    If things don't work they way you think it should work is normally because a lack of communication/proper explanation.
    You have the workbook in front of you while all we have is the attachment from Post #1 which might be close to reality but quite often things are different.

  21. #21
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,523

    Re: Detect first and last row and count blanks in between

    If you run this first
    Please Login or Register  to view this content.
    and this after

    Please Login or Register  to view this content.
    does it work?

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Quote Originally Posted by Musto85 View Post
    Is there a way that can be slightly adjusted to give the total of blanks cell for each column at the bottom instead of the message box?
    Try change to
    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    That’s great thank you so much works brilliantly.
    Is there a way that the averages can be rounded down to the nearest whole
    i.e 4.5 to 4,
    2.5 to 2….

  24. #24
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Edit;
    Round down.
    Last edited by jindon; 05-15-2022 at 12:38 AM.

  25. #25
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Amazing.
    Last one I promise
    I can’t make it run for various sheets at the same time
    My sheets are called:
    Mon
    Tue
    Wed
    Thu
    Fri
    Sat
    Sun

  26. #26
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Replace "test" sub procedure with below
    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Brilliant! Thank you jindon💪🏻

    Thanks to jolivanes too and all involved 👌🏼

  28. #28
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    I will have to reopen this thread as I’m now facing another challenge…
    I have attached a sheet as example.

    As you can see from the attachment I have another set of data at the bottom (A35:U59) with numbers in between certain time intervals.
    What I would like to obtain is comparing the data at the bottom to the data at the top (A1:R25) ensuring the relevant columns correspond with the label row i.e. LAR01 with LAR01, LAR06 with LAR06 etc…(as this can be misplaced in different columns) fill with colour where the corresponding interval has no data and also the average between first and last number where the interruption is.

    I.e. LAR01 (B35:B59) has numbers between 6:00pm and 1:00 am, on the data above same column corresponding label LAR01 there is data missing at 9:00pm, 10:00pm and 01:00am and so on for all the columns (I’eve filled as example until LAR19.


    Sorry for the explanation, please feel freee to ask for more examples if needed.

    Thank you in advance!!
    Attached Files Attached Files

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Quote Originally Posted by Must85
    also the average between first and last number where the interruption is.
    I don't understand the calculation above.

    This is to only color the cell(s).
    Please Login or Register  to view this content.

  30. #30
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    I couldn't expect anything less than a work of art from you jindon!

    That works perfectly thank you.
    Now I would need to count those highlighted cells placing the total for each column at the bottom (B27,C27 etc..) and fill these highlighted cells with the average between the numbers on the above and below cells i.e. B17 and B18 = average between 5 and 0.
    B21 = average between 4 and 0 and so on...

    I used this code that you provided before that was doing exactly that but I believe will need tweaking...

    Please Login or Register  to view this content.

  31. #31
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    If you upload a workbook with before/after sheets clearly showing how you want, it will be clear.

  32. #32
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Please see attachment.

    As you will notice I'm trying to avoid filling with zeros... so when number above and below are zeros it will have to fill with a 1 instead of zeros.
    Attached Files Attached Files

  33. #33
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    See if this is how you wanted.
    Please Login or Register  to view this content.

  34. #34
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    The average and the count at the bottom works fine however it also fills the cells that are not highlighted in red.


    i.e. I12:I15 have been also filled with average
    M14:M17 too...

  35. #35
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Try this
    Please Login or Register  to view this content.

  36. #36
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Brilliant, thanks jindon!

    Is there a way to have a message box appearing for every single hour that shows all those numbers in the highlighted cells with the corresponding label?
    i.e.
    6:00am no msgbox to shown
    07:00
    LBJ07 = 1
    LPB07 = 1
    11:00 am
    LTCP01 = 1
    12:00pm
    LPB02 = 3

    ...and so on for every hour, skipping where there are no highlighted cells.
    When Pressing "ok" on the msgbox it will close and the next one will open.

  37. #37
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    try
    Please Login or Register  to view this content.

  38. #38
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Re: Detect first and last row and count blanks in between

    Thanks jindon

    Instead of going by column can it go by row and therefore by each time interval:

    06:00:
    07:00: LBJ07 = 1, LPB01 = 1
    08:00:
    09:00:
    10:00:
    11:00: LTCP01 = 1

    And so on…

  39. #39
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Detect first and last row and count blanks in between

    Please Login or Register  to view this content.

  40. #40
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101
    Great, but the code before for some reasons was showing the msgbox right after highlighting and filling all the cells with average.
    This last code instead it highlights everything and fills with average after clicking “ok” on the msgbox.

    Is there also a way to create a new sheet with the info contained in the msgbox?

    Thanks for helping me out with this jindon much appreciated.

+ 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. count non blanks and blanks frequency in chunks
    By egret in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-06-2019, 08:13 AM
  2. [SOLVED] Formula to count blanks and non-blanks with a dynamic range
    By brittdyer in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-12-2018, 07:45 AM
  3. [SOLVED] detect and count # of cells to use in formula
    By jrtaylor in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-16-2017, 10:53 PM
  4. does power pivot count blanks when it does a count?
    By stephme55 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 01-26-2016, 09:16 PM
  5. Replies: 5
    Last Post: 08-29-2012, 05:25 PM
  6. How to automatically detect the count of filled colums
    By Nerella in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2012, 01:48 PM
  7. detect cell color and count it
    By Maileen in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-31-2005, 07:09 PM

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