I have a file........see enclosed.
I want to count the number of non-blank cells in a range from A to Z along a given row.
Then once the loop is done i want it to tell me the row which had the most non-empty cells but its not working.
I have a file........see enclosed.
I want to count the number of non-blank cells in a range from A to Z along a given row.
Then once the loop is done i want it to tell me the row which had the most non-empty cells but its not working.
Hi welchs101, how you display this is up to you but the following procedure will count everything (even the headers).![]()
Sub NonBlanks() Dim RngToSrch As Range, cl As Range, RngCnt As Long Set RngToSrch = Sheets(1).Range("C5").CurrentRegion RngCnt = 0 For Each cl In RngToSrch If cl.Value <> "" Then RngCnt = RngCnt + 1 Next cl MsgBox "The count is: " & RngCnt End Sub
If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.
---Keep on Coding in the Free World---
FYI - you can use WorksheetFunction.CountA() to count the number of non-empty cells in a given range.
If you want a count of both empty and non empty cells, try:![]()
Sub NonBlanks() Dim RngToSrch As Range, cl As Range, RngCnt As Long, EmptyCnt As Long Set RngToSrch = Sheets(1).Range("C5").CurrentRegion RngCnt = 0 EmptyCnt = 0 For Each cl In RngToSrch If cl.Value <> "" Then RngCnt = RngCnt + 1 Else: EmptyCnt = EmptyCnt + 1 End If Next cl MsgBox "The sum of cells with values are: " & RngCnt & vbNewLine _ & "The sum of cells with no values are: " & EmptyCnt End Sub
Wow that shortens the code!Nice one Colin!![]()
Sub NonBlanks() Dim RngToSrch As Range, cl As Range, RngCnt As Long Set RngToSrch = Sheets(1).Range("C5").CurrentRegion RngCnt = 0 Debug.Print WorksheetFunction.CountA(RngToSrch) End Sub
hi Mordred,
your code is cool!
But i was trying to count the number of non-blank cells in a given range and that range was a row from about A to Z. I then wanted to know which row had the most non-blank entries. Your code, while very very cool (and i can use it in another place), counts the number of non-blank cells in the "entire" range of a current region where c5 is in that region. I am not guaranteed that c5 is in the region of interest.
Hi welchs101, The following code works base on you selecting a whole row and then running the code, which is also based on the workbook you provided where the first fill column is at C.![]()
Sub NonBlanks2() Dim rNum As Long, rAddress As String Dim LastCol As Long rAddress = Selection.Address rNum = Range(rAddress).Row LastCol = Cells(5, Columns.Count).End(xlToLeft).Column Debug.Print WorksheetFunction.CountA(Range(Cells(rNum, 3), Cells(rNum, LastCol))) End Sub
thanks for your help
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks