How can I count the rows with data in an Excel sheet? - vba

I am trying to count the number of rows in a spreadsheet which contain at least one non-blank value over a few columns: i.e.
row 1 has a text value in column A
row 2 has a text value in column B
row 3 has a text value in column C
row 4 has no values in A, B or C
The formula would equate to 3, because rows 1, 2, & 3 have a text value in at least one column. Similarly if row 1 had a text value in each column (A, B, & C) this would be counted as 1.

With formulas, what you can do is:
in a new column (say col D - cell D2), add =COUNTA(A2:C2)
drag this formula till the end of your data (say cell D4 in our example)
add a last formula to sum it up (e.g in cell D5): =SUM(D2:D4)

If you want a simple one liner that will do it all for you (assuming by no value you mean a blank cell):
=(ROWS(A:A) + ROWS(B:B) + ROWS(C:C)) - COUNTIF(A:C, "")
If by no value you mean the cell contains a 0
=(ROWS(A:A) + ROWS(B:B) + ROWS(C:C)) - COUNTIF(A:C, 0)
The formula works by first summing up all the rows that are in columns A, B, and C (if you need to count more rows, just increase the columns in the range. E.g. ROWS(A:A) + ROWS(B:B) + ROWS(C:C) + ROWS(D:D) + ... + ROWS(Z:Z)).
Then the formula counts the number of values in the same range that are blank (or 0 in the second example).
Last, the formula subtracts the total number of cells with no value from the total number of rows. This leaves you with the number of cells in each row that contain a value

If you don't mind VBA, here is a function that will do it for you. Your call would be something like:
=CountRows(1:10)
Function CountRows(ByVal range As range) As Long
Application.ScreenUpdating = False
Dim row As range
Dim count As Long
For Each row In range.Rows
If (Application.WorksheetFunction.CountBlank(row)) - 256 <> 0 Then
count = count + 1
End If
Next
CountRows = count
Application.ScreenUpdating = True
End Function
How it works: I am exploiting the fact that there is a 256 row limit. The worksheet formula CountBlank will tell you how many cells in a row are blank. If the row has no cells with values, then it will be 256. So I just minus 256 and if it's not 0 then I know there is a cell somewhere that has some value.

Try this scenario:
Array = A1:C7. A1-A3 have values, B2-B6 have value and C1, C3 and C6 have values.
To get a count of the number of rows add a column D (you can hide it after formulas are set up) and in D1 put formula =If(Sum(A1:C1)>0,1,0). Copy the formula from D1 through D7 (for others searching who are not excel literate, the numbers in the sum formula will change to the row you are on and this is fine).
Now in C8 make a sum formula that adds up the D column and the answer should be 6. For visually pleasing purposes hide column D.

You should use the sumif function in Excel:
=SUMIF(A5:C10;"Text_to_find";C5:C10)
This function takes a range like this square A5:C10 then you have some text to find this text can be in A or B then it will add the number from the C-row.

This is what I finally came up with, which works great!
{=SUM(IF((ISTEXT('Worksheet Name!A:A))+(ISTEXT('CCSA Associates'!E:E)),1,0))-1}
Don't forget since it is an array to type the formula above without the "{}", and to CTRL + SHIFT + ENTER instead of just ENTER for the "{}" to appear and for it to be entered properly.

Related

Sum of Named Ranges to Array in Excel

I have values 1,2,3 and 2,3,4 in columes A and B respectively. I want column C to be 1+2, 2+3, 3+4. I have named the first 3 cells of column A as RANGE_A and the first 3 cells of column B as RANGE_B
I have tried sum(RANGE_A, RANGE_B), but that gives me the actual total of 15 in every cell of the output range.
I don't want to do this in VBA, and it would be cleaner if I can use the ranges like I have tried, but if all else fails i'll be just using A1+B1,A2+B2 etc..
***** EDIT ********************************
Where you want to sum two named ranges ={sum(RANGE_A, RANGE_B)} produces a scalar value, reflected in every cell of the output array, equal to the sum of both columns.
My solution is in fact, incredibly simple (thanks to QHarr, who got this right even when my original question was wrongly written!)
={RANGE_A + RANGE_B} produces an output array where each value is the sum of each pair of cells in each range. That's all i was looking for!
Cheers
J
You want to select C1:C3 and enter the following in the formula bar:
=RANGE_A*RANGE_B
then press Ctrl + Shift + Enter to enter as an array formula.
For addition you can use:
=RANGE_A+RANGE_B

Using CONCATENATE to populate a sum formula in excel vba

I have a spreadsheet that has numbers for a particular year and then subtotals for that year.
The number of instances in a year can vary and there may be a year that doesn't exist i.e., 2018 might be skipped. The title of the totals row is always "FYXX Totals." I have a for loop that goes through the entire column and looks for "FY" Then if it falls within one of three categories (FY1-FY2, FY3-FY7, FY3-FY9; these being variables that represent a year). What I need is for the loop to sum the number in column D, E, F...when "FY" is found. I think using CONCATENATE might be the way to go but I am not sure a) exactly how to do that or b) if that is even the best way to go about it.
Dim rng As Range
Dim SumRow As Integer
Set rng = Range("C4:C" & NextRow)
For Each cell In rng
If Left(cell.Value, 2) = "FY" Then
If 2000 + Int(Mid(cell.Value, 3, 2)) <= FY2 Then 'This is the if statement for the fisrt category
'Here would be the sum function when the if statement is triggered
End If
End If
Next cell
Thanks so much for the help.
SpreadSheet_Picture
The second if loop (right now written for the first category) would need to sum the just the zero that is in column D next to FY17 Total. Keep in mind that sometimes row for FY18 may exist so this macro would have to be able to grab that as well should it exist. But in this case what would have to go into cell "D" & NextRow would be =SUM(D5).
My best attempt at understanding what you're after is that you want to place a formula in each cell of column D where the cell in column C contains FYxx Total, where xx meets certain other criteria. That formula should contain the sum of all the cells in column D for which the cell in the corresponding row of column C contains the same value of FYxx.
The easiest way I can think of to achieve this is to use the SUMIF function:
The formula =SUMIF(B$2:B$999,LEFT(C3,4),D$2:D$999) in cell D3 calculates the sum of all cells in column D where the cell in the corresponding row of column B matches the criterion, i.e. is equal to the first four characters of cell C3.
Unless there are further instances of the same FYxx value elsewhere in column B, the ranges in the first and third arguments of the SUMIF can cover the whole table, which I've assumed here extends to row 999, so you can keep those the same for each cell you place this formula in. You only need to change the row for the cell in column C in the second argument.
In fact if you really want, you can place exactly the same formula in each Total cell in column D:
=SUMIF(B$2:B$999,LEFT(INDEX(C$2:C$999,ROW()-1),4),D$2:D$999)
Here the INDEX function looks up the appropriate cell in column C based on the row of the cell that the function is placed in.

Return values from other workbook

Have a question about formula which will resolve my issue.
In my main workbook I need to compare data from two sources.
One of the columns must retrieve data(amounts) from other workbook.
I want formula which will search for all amounts in column G and will skip all blank cells. Tried to use VLOOKUP, INDEX and SMALL functions but no effect.
Each day amounts are different and I need to match them in main file and find exeptions.
Any ideas?
How about an array formula such as the following?
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-1)-ROW($G$2:$G$20)+1)
The formula would have to be placed into cell I2 as an array formula (which must be entered pressing Strg + Shift + Enter). Then you can drag down the formula to get all the other values.
It doesn't have to be in column I but it has to be in row 2 because this formula get's the n-th Number from the list which is not = 0. The n-th place is (in this formula) row()-1. So for row 2 it will be 2-1=1 and thus the 1st number. By dragging down the formula you get the 2nd, 3rd, etc. number. If you start with the formula in cell I5 instead then it would have to be adjusted to be as follows:
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-4)-ROW($G$2:$G$20)+1)
You could loop through the column and store each value >0 in an array and then compare or you loop through the column and compare directly...
something like:
Dim i as Integer = 0
Foreach value in Maintable
Do
If otherworkbook.cells(i,7) = value Then '7 for G
do your stuff
End If
i = i + 1
While i < otherworkbook.rows.count
Next
I think that could be the right approach

Excel VBA script to count the number of values in column A that are less than the values in Column B same row

Sub CompareValues()
Dim rng As Range
Dim cellA As Object
Dim cellB As Object
Dim count As Integer
Set rng = Selection
For Each cell In rng
'test if value in column A is less than in column B
If cellA.Value < cellB.Value Then
count += 1
End If
Next
End Sub
Basically I have a spreadsheet with 50+ columns that each have 350 rows with mostly numeric values (the values will either be a number or NA if a result wasn't available). I want to be able to generate a count at the bottom of each column that tells me how many of the values in a column are less than the equivalent values in the previous column. So if B3 < A3 the count would go up. But if B3=A3 or B3 > A3 then it wouldn't iterate the count. I know I need to loop through each value and compare it to the value in the same row in the previous column but I'm pretty lost on how to actually do that. Any help would be greatly appreciated.Thanks.
Simple solution just using formulas. At the bottom of each column, for example on B351 you would use the array formula:
=SUM(IF(A1:A350<=B1:B350,1,0))
Note that this is inputted by pressing CTRL + SHIFT + ENTER. Regards,

Move from one cell to another and count the number of rows in between two data

I have got one column with 250 rows. The data fed is 0 and 1 randomly in these rows. I need to find the number of row between any two 1's. I need to write a macro for the same.For ex: if my column is as follow:
A1 0 B1 2
A2 0 B2 2
A3 0
A4 1
A5 0
A6 0
A7 1
A8 0
A9 0
A10 1
What I want is to check the entire column from A1 to A10. Starting from A1, my program would check for '1', it found in A4, it will continue to move down until it finds another '1'. Here it will move to A7. Now the result should be 'number of rows in-between' i.e 2 in this case. This result should be pasted in another column, lets say B1. This process need to be continued till my cursor reaches the B10 row. Please help.
This should do what you are after. It assumes that the data in column A is contiguous.
Sub helpSandeep()
Dim c As Range
Dim counting As Boolean
Dim zeroCount As Long
[B:B].ClearContents
[B1] = "Results"
For Each c In Sheets("Sheet1").[A1:A65536].Cells
If Len(c) = 0 Then
'Assuming that an empty cell means we are finished
Exit For
Else
If c.Value2 = 1 Then
'Update the results and reset the count
counting = True 'Avoids counting any initial zeroes as per your example
If zeroCount > 0 Then
[B65536].End(xlUp).Offset(1, 0) = zeroCount
End If
zeroCount = 0
Else
'Increment count, assuming we are actually counting
If counting Then
zeroCount = zeroCount + 1
End If
End If
End If
Next c
End Sub
Enter with Control+Shift+Enter to make it an array formula. In B1
=LARGE((ROW($A$1:$A$10))*($A$1:$A$10=1),SUM(--($A$1:$A$10=1))-(ROW()))-LARGE((ROW($A$1:$A$10))*($A$1:$A$10=1),SUM(--($A$1:$A$10=1))-(ROW()-1))-1
and fill down. The formula consists of subtracting two LARGE functions and then subtracting 1 from the results.
The first argument to the LARGE functions is an array that returns the row number for every entry that contains a 1.
(ROW($A$1:$A$10))*($A$1:$A$10=1)
The second argument to the LARGE functions is a SUM array formula that counts all the entries with a 1, then subtracts the current row number (or the current row number -1). For a formula in B1, this would return 3-1 (or 2, the count of all entries with one (3) less the row number (1)).
For the second LARGE function, the second argument would return 3-0 (3).
All of that says, give me the 2nd largest row for the entries that contain a 1 and subtract the 3rd largest row for the entries that contain a 1.
When you fill down to B2, that becomes: give me the 1st largest row for the entries that contain a 1 and subtract the 2nd largest row.
Finally we subtract 1 from the whole thing to make the result exclusive (rather than inclusive) of the rows that contain the 1's.
I know you asked for a macro, but you may find formulas more suitable.