excel countif entire row in a table - excel

I have a structured table in excel 2016.
I want to have a cell to count the number of cells across the entire row within the table if it matches my criteria.
I have tried putting this formula in column A on each row =COUNTIF(Table[#],"my criteria") but that does not count properly.
However, this works: =COUNTIF(Table[#[ColB]:[ColH]],"my criteria"). But since my table will expand, I don't want to specify the column name [ColB], I want to refer to the entre row in the table.
A header
countif
colC
colD
colE
First
formula
A
C
Second
formula
B
C
formula = =COUNTIF(Table[#],"A") does not work
formula = =COUNTIF(Table[#[colC]:[colE]],"A") works
My table will expand both horizontally and vertically.

Please Note: This solution is only available if you chose to use VBA. VBA does not work in web versions of Excel. Please ensure that you resave your workbook as a macro-enabled workbook before proceeding.
You can choose to use VBA and create your own custom worksheet formula. Since this question didn't start out as a VBA issue, I will be a bit more detailed on the process of setting this up as opposed to just throwing you some code and you having to figure out what to do with it.
After you've re-saved your workbook as a macro-enabled workbook, open the VBA Editor (VBE) by pressing Alt and F11 simultaneously.
In the VBE, click Insert > Module. You should now see Module1 highlighted on the left side bar (The project Explorer).
Copy and paste the following code in the blank area of the module:
Option Explicit
Public Function GetMyRowCount(Criteria As Variant) As Long
Dim ws As Worksheet
Dim tblRng As Range, RowRng As Range
With Application.Caller
Set ws = .Worksheet
Set tblRng = ws.Range(.ListObject.Name)
Set RowRng = ws.Range(ws.Cells(.Row, .Column + 1), ws.Cells(.Row, tblRng.Columns.Count))
End With
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
End Function
Now use this UDF (User Designed Function) in your worksheet. In the column you would like the calculation to be in, simply type =GetMyRowCount("My Criteria") and it should calculate.
To point out how this code works in more detail:
Application.Caller is referring to the cell that this function is located in. Because we now know the location of the cell, VBA can use it's location to obtain the row data from it (which is why you don't need an argument for the row #).
RowRng is getting the starting point of the column within the ws.Range(...) function with the first ws.Cells(...) function. .Row is the row # from the GetMyRowCount function (using Application.Caller.Row method), and the 3 is simply the static column C.
The way we grab the last column we need is by counting the total # of columns within the table: ws.Cells(.Row, tblRng.Columns.Count)
Using the information we obtained from bullets 2 and 3, we can establish the entire range of the lookup we need, and then place this range into your CountIf() function, along with the criteria you passed with the function's argument:
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
As you can see in the following example, I wanted to count the number of times in the row the number 1 occurred:
Another example showing it works with text as well by using "Apple" as the criteria:

Try this: =COUNTIF(B:B,"my citeria"), so if your Column is A, range would be A:A, for B it is B:B.
Let me know if this helps.

Related

how do I get value from a sheet then show on another sheet in excel base on matching id

I have an ID and Name column in sheet A and in sheet B I have an ID column and an empty column
what I want to do is to fill the empty column with values from the Name column in sheet A where both the ID columns are matched
here is what I want to do in MySQL statement
select SheetA.Name from SheetA join SheetB on SheetA.id = SheetB.id
can someone help translate this to excel code?
Refer to screenshots when interpreting the ranges (which will require modification to suit your specific needs / Excel data list positioning)...
VB code:
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveCell.Formula2R1C1 = _
"=INDEX('67650244 (2)'!R4C3:R7C3,MATCH('67650244'!R4C2:R7C2,'67650244 (2)'!R4C2:R7C2,0))"
Range("C5").Select
ActiveWindow.SmallScroll Down:=-3
End Sub
Screenshots
Sheet 1 (entitled "67650244"):
Sheet 2 (entitled "67650244 (2)"):
This assumes the source values you're looking up from are contained with the 2nd sheet (i.e. "67650244 (2)"), and the index/match function "places" the relevant ID/lookups into Sheet 1 (i.e. "67650244"). Further, the "activecell" is simply cell "C4" in sheet 1 (the function in the VB code above will insert the response/'looked-up' values as an array to fill all values adjacent to those in column B of this sheet (assuming a successful lookup, if not, then it will display "#Value!" next to the relevant Lookup value that couldn't be found in sheet 2....
Note: VB using "Row/Column" referencing when using a function like this, where "R1C1! simply refers to cell A1 in excel. To decipher this more easily for writing up the applicable version of code for your case, you can go to "Tools, Options, Formula" in the left had pane (within Excel, not VB), and then select "R1C1 Reference Style" box, per screenshot below:
You can always change this back later (deselect the option) as desired the same way.
Note: it would be far easier / more convenient to just use the index(range to return, match(value to lookup, range to lookup, 0)) method within excel as opposed to within VB - but this depends on desired functionality of your setup/circumstances/excel model/etc.

vba offset with reference to cell

I need to copy a column of data to another column on a different worksheet, pasting values only. The appropriate paste column is identified in a single cell. This cell will be changed manually each time the macro is applied. So one time I might want to copy/paste in the first column, so my identifier cell is 1. The next time I might input 5 in this cell so that I offset 5 columns to the right to copy/paste data. Thank you.
You can reference the columns in a worksheet using the Columns property. You can achieve what I think you're trying to do with code like this.
Dim col As Integer
col = SomeSheet.Cells(1,1).Value
FromSheet.Columns(col).copy
ToSheet.Columns(col).PasteSpecial xlPasteValues

Excel: How to refer to entire column in a formula by column title?

I want to write a formula like =SUM(tab2!A:A) but instead use the column title of A which is say "count". How can I modify this to look more like: =SUM(tab2!"count")?
The reason I want to do this is because I copy and paste a spreadsheet from another source in tab2 and the column referring to "count" may be in a different column. I want the formula to give me the correct calculation as soon as I paste the new spreadsheet by automatically finding the column to sum up by title name.
I must be missing something because this seems like a very basic question and I can't find the answer anywhere...
Thanks for your help!
I like the idea of naming ranges proposed by #Doug, but if the issue is that you are dumping your data in [and you don't know in advance which column is going to be where] and would need to rename your range every time, there are other options - I suggest using OFFSET. OFFSET allows you to dynamically create a range, starting at a particular point and moving down/up / right/left for as many rows and columns as you determine.
In your case, you will need to combine that with a method for searching the columns to see which one says "Count". I am assuming that your column headings are always in row 1. If they aren't [or if they aren't always in row 2, or any row you know in advance]... you can get around that but then I'd recommend you try to make your data more uniform instead of creating unnecessary Excel workarounds.
In total your formula would look like this:
=SUM(OFFSET(A1,,MATCH("Count",1:1,0)-1,ROWS(A:A),1))
What this does is first determine which column the word "Count" is found in, in row 1. Then it subtracts 1 from that number - this now equals the number of columns to the right that it is, from column A. It uses offset to create a reference to that column, for all rows, and sums those rows together.
Check out the name manager for naming ranges :)
You didn't say whether you would consider a macro solution. If so, this may work.
If the sheet you are pasting into is Sheet2 and the sheet you want the result in is Sheet1, then this macro, if placed in the Worksheet_Activate event of Sheet1 will give you the result as soon as you click on the Sheet1 tab afetr pasting your data into Sheet2:
Private Sub Worksheet_Activate()
Dim r As Range
Dim rCol As Range
Dim rFound As Range
Dim ws As Worksheet
Dim lTotal As Long
Set ws = Sheet2
Set r = ws.Cells
Set rFound = r.Find("count")
If Not rFound Is Nothing Then
Set rCol = rFound.EntireColumn
lTotal = Application.WorksheetFunction.Sum(rCol)
End If
Cells(1, 1) = lTotal
End Sub
It does assume there is only one cell with the word "count" in it on Sheet2.

How to use one column in excel as a search parameter in another column to output only certain strings

This question stems off another post I had. (see Search through column in excel for specific strings where the string is random in each cell)
Using the above image as reference, I am trying to search through column B (actually over 1000 lines) using column E as the "lookup values." The end goal would be for "just" the names to be displayed in column C. The trick is all the randomly generated characters the encompass the names. Below is what I would want the datasheet to look like. A formula or module should work, but the vlookup and other lookup function I can't get to work.
For a worksheet function approach, you could enter in C3 and fill down this formula:
=LOOKUP(8^5,SEARCH(E$3:E$7,B3),E$3:E$7)
The constant 8^5=32768 is chosen to be larger than the maximum possible string length so that LOOKUP returns the last matching value. The formula returns #N/A if no string is found.
Another possibility, which may be easier to understand then assylias post initially, but also may be a bit more time consumptive (although with 1,000 rows, I don't think it will matter much) is below.
This requires that you name the range in column E as myNames (or whatever name you wish, just update the code - alternatively, you cuold just write Range("E1:E6")). Also, if you move the random values from column B, update that in the code as well.
Sub findString()
Dim celString As Range, rngString As Range, celSearch As Range, rngSearch As Range
Dim wks As Worksheet
Set wks = Sheets("Sheet1") 'change sheet reference to whatever your sheet name is
Set rngString = wks.Range("myNames")
Set rngSearch = Intersect(wks.UsedRange, wks.Range("B1").EntireColumn)
For Each celString In rngString
For Each celSearch In rngSearch
If InStr(1, celSearch.Text, celString.Value) > 0 Then
celSearch.Offset(, 1) = celString.Value
End If
Next
Next
End Sub
Since, I worked on your original question as well, I would suggest getting the counts through Siddharth's answer and then running this, or assylias's code above to get the names next to the columns. You could put a button the sheet, or just use the Macro dialog box to run the macro.

Excel VBA Set Cell Formula Error

This one has me stumped. When I set the formula for a selected cell in a ListObject, if the ListColumn is empty, Excel fills the formula for the whole column, rather than just the Selection. I have duplicated this in a separate workbook.
Create a Table
Insert a five or so rows
Click on (select) one of the cells in the column
Execute the following code:
Sub setCellFormula()
Selection.Formula = "=myFormula()"
End Sub
Function myFormula() As Integer
myFormula = 1
End Function
Note that the whole column is filled with the numeral one
Delete the data in the cells
Enter any value in any of the cells
Select a cell other than the cell with a value in it
Re-execute step 4
Note that only the selected cell is filled with the numeral one
My experience is that, regardless of VBA, Tables behave as you have described in XL 2007, and Lists behave as you'd prefer in XL 2003. However in 2007 you can tell it to "stop creating calculated columns." So I'd guess it's not a code issue, but an issue due to different behavior between versions, or different settings for two instances of XL 2007.

Resources