Marking repeating or both in corresponding cell based on the criteria - vba

i have a sheet in excel which have two columns Col "A" & Col "B"
in col "A" i have values like 1,1,1 - 3 times & 2,2,2,2 - 4 times
and in col "B" i have yes, yes, yes corresponding to column A value 1,1,1 &
yes, no ,yes no corresponding to column "A" value 2,2,2,2 as shown in the image.
now i want to add some values in col "C" based on codition like
if col "A" 1,1,1 all values are yes in col "B" then put SOME TEXT LIKE "repeating" in Col "C" in front of every value of col"A"
and if col "A" 2,2,2,2 all values are different like yes, no, yes , no in col "B" then put SOME TEXT LIKE "both" in Col "C" in front of every value of col"A"
for example see below image.-
-it will check all the similar values in col "A" First suppose it will take "1" first then it will check its corresponding value in col "B" and it will keep on checking all of the values of "1" in col"B" if all values are "yes" then it will simply put "repeating" in col"C" in front of every "1"
-but again it will check for all the values of "2" in col"B" if all values are "yes" then it will simply put "repeating" in col"C" in front of every "2" but as you can see all values are not same in front of "2" some are "yes" and some are "no" in col"B" so if all values of "2" are not same in col "B" then it will put "both in col"C" in front of every "2"
i want to know is there any formula for doing this or vba code is needed . please help me regarding this.

If you have Excel 2016 you can use COUNTIFS nested in an IF like:
Countifs will count the values based on two criteria.
=IF(COUNTIF($A$1:$A$7,A1)=COUNTIFS($A$1:$A$7,A1,$B$1:$B$7,B1),"Repeating","Both")
You can nest a third condition like this:
=IF(B1="may be","Confirm",IF(COUNTIF($A$1:$A$9,A1)=COUNTIFS($A$1:$A$9,A1,$B$1:$B$9,B1),"Repeating","Both"))
Explanation:
The formula consists of two conditional IFnested:
The first IF(B1="may be","Confirm" evaluates if B1 = "may be" when is
true it returns "Confirm".
when is false start the second IF to compare one condition:
COUNTIF($A$1:$A$9,A1) - Countsif function counts values in a range
based on one condition. In this case, the criteria are each cell in
column A and count how many times are this value in the range
$A$1:$A$9.
COUNTIFS($A$1:$A$9,A1,$B$1:$B$9,B1) - Countifs function counts
values in a range based on multiple conditions. In this case, it is
counting based on two criteria for each value in A and B.
Evaluating second conditional:
For C1:
COUNTIF($A$1:$A$9,A1) = 3 3 times the value (1) in the range
$A$1:$A$9.
COUNTIFS($A$1:$A$9,A1,$B$1:$B$9,B1) = 3 3 times the values (1) and (yes).
So, if 3 = 3 then "Repeating".
For C4:
COUNTIF($A$1:$A$9,A4) = 4 4 times the value (2) in the range
$A$1:$A$9.
COUNTIFS($A$1:$A$9,A4,$B$1:$B$9,B4) = 2 2 times the values (2) and (yes).
So, if 4 = 2 is false it returns "Both".

Related

Query returning 1st cell in range although condition is not met (Google Sheets)

I'm a first time query-user, so I'm out of my depth for fixing this myself.
Here's what I have and am looking to accomplish in two separate Google Sheets spreadsheets:
Spreadsheet1 - column A contains a list of names, column B contains a checkbox for true or false
Spreadsheet2 - import list of names from Spreadsheet1 column A where column B is true (and transpose the names so they appear in columns)
This is the formula I'm using in Spreadsheet2:
=TRANSPOSE(QUERY(IMPORTRANGE(LinkToSpreadsheet1,"Sheet1!A39:B51"),"Select Col1 where Col2 = TRUE",1))
The problem I'm facing is:
When column B in Spreadsheet1 contains only FALSE results, the formula is still returning cell A39 as the result, rather than returning no result.
I've also tested that when column B in Spreadsheet1 does contain a TRUE result (for example cell B48 = TRUE), but A39 is FALSE -- it is still returning cell A39 in addition to the matching TRUE cell of A48.
The problem I'm facing is:
When column B in Spreadsheet1 contains only FALSE results, the formula is still >returning cell A39 as the result, rather than returning no result.
that's because you use 1 as 3rd query parameter. you should use 0. try:
=IFERROR(TRANSPOSE(QUERY(IMPORTRANGE(LinkToSpreadsheet1,"Sheet1!A39:B51"),
"select Col1 where Col2 = TRUE", 0)))

WHERE condition is true but not all rows are returned? (SQLite db)

When I try to use WHERE word = "a" there is only 1 row returned when in fact there are 2 rows with the word "a". As you can see in the second picture there is also another "a" with the id = 1. How can I query both of them?
Only 1 row is returned with WHERE word = "a"
There are actually 2 rows with for the word "a"
UPDATE: They actually have different length,
one 1 and the other 2.

How to create column having values repeated for specific number of times

Have to create a column next to existing column. my present table is like this
and result needed is like this. Each date value is getting repeated 3 times and getting incremented by 1
Pick any cell and enter:
=DATE(2018,8,21+ROUNDUP(ROWS($1:1)/3,0))
and copy downward.
Here our start cell is one cell to the left of the 21.
Manually type in the first 3 into A1:A3 (or rows 1 to 3) in A4 (or row 4) reference A1 (row 1) and add 1 to it. Then drag the formula down.
The formula should look like =A1+1

Compare two sheets using ID column

I am looking to do a comparison of 2 sheets in a workbook in Excel 2013. Due to the number of records VLOOKUP and other formulas have been slow so I thought I would try VB to see if this was a quicker solution.
What I would like to do is compare each record by ID and highlight and mismatches in red. Due to the column names and position being different, I would also like to do the cell comparison on each record by specifying the column names to compare against. Finally, I would like to total the mismatches for each column into a 3rd sheet.
For example:
Sheet 1:
ID Col1 Col2 Col3 Col4
1 1 1 1 1
2 2 2 2 1
3 3 3 3 3
4 4 4 4 4
Sheet 2:
DBID Col1 Col2 Field Col3
1 1 1 1 1
2 2 2 2 2
4 4 4 4 4
3 3 3 3 3
So in the above example I would only like to Col4 compared with Field column and only see the Field column for ID 2 highlighted as an error with ID records 3 and 4 ignored because they match and are just in different positions in the file.
I would normally sort on ID instead of picking out a particular ID, but conscious that there could be records missing which means the data would be misaligned.
At the moment I have found this code which will highlight the mismatches in red, but matches cell by cell without taking into consideration that the columns and records might not be in the same order.
Sub RunCompare() 'Call the compareSheets routine Call compareSheets("Sheet1", "Sheet2") End Sub
Sub compareSheets(shtBefore As String, shtAfter As String) Dim mycell As Range Dim mydiffs As Integer 'If current cell is not a date then proceed (else skip and go to next), then 'if not same as corresponding cell in sheet After, 'mark as yellow and repeat until entire range is used For Each mycell In ActiveWorkbook.Worksheets(shtAfter).UsedRange If Not IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtBefore).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
End If End If Next 'Display a message box stating the number of differences found MsgBox mydiffs & " differences found", vbInformation ActiveWorkbook.Sheets(shtAfter).Select End Sub
I am assuming that the ID is unique.
You have basically two solutions, with and without macro.
With Macro Logic can be as follows :
Get the first (Unique) column of first sheet
Loop through the first (Unique) column and find the matching row in second sheet
Compare between cells in that row with the first row of first sheet
Repeat the same steps for all rows
Also do a check to see if both sheets have same number of rows and columns; and no rows are duplicated.
Non Macro Solution :
Use VLookup Function to lookup for the row matching the value and do an equal comparison formula in a new sheet as
=IF(Sheet1!B1=VLOOKUP(Sheet1!A1,Sheet2!A:Z,2,FALSE),"Same","Different")
Note that you will need to increment the row number and column name I have highlighted in first column of the third (Answer) sheet.
Once you have values, you can use conditional formatting to highlight Different to Red

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

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.