Apply Count formula in a Excel Row with conditions True/False - sql

I have to apply a logical count formula on a column which contains some value. It contains identical and non-identical numbers.
What I want is a way to apply a formula to the next column which will lookup the whole column and decide on below condition:
whether A1=A2; if True, if A1=A3 and so on till it returns False (not equal value) then count all the true results in a row and reflect the value against the first cell.
Then if there was three match then it should reflect 3 against the first cell and the next two cells should be left blank. Below is the example column:
Numbers No of Days
47.76
-429.98
-429.98
-429.98
-1328.98
-1328.98
-1328.98
-833.32
-643.7
-1328.98
-580.26
-556.76
-556.76
-1181.78
-1139.99
-1124.49
-1103.59
-1103.59
-1091.39
-1048.94
-1048.94
-451.38
-451.38
-321
-321
438.5
477.5
698.76
795.93
795.93
804.83
What I want should be like below after formula:
Numbers No of Days
47.76 1
-429.98 3
-429.98
-429.98
-1328.98 3
-1328.98
-1328.98
-833.32 1
-643.7 1
-1328.98 1
-580.26 1
-556.76 2
-556.76
-1181.78 1
-1139.99 1
-1124.49 1
-1103.59 2
-1103.59
-1091.39 1
-1048.94 2
-1048.94
-451.38 2
-451.38
-321 2
-321
438.5 1
477.5 1
698.76 1
795.93 2
795.93
804.83 1
Please help guys as whatever I tried has failed here. Need some excel experts to share some advise or a resolve.

In Cell B2 use =IF(A2=A1,"",MATCH(0,--(A2=$A2:$A$50),0)-1) entered using ctrl+shift+enter
The if statement checks to see if the number is the same as the one above it and if it is it leaves the cell blank.
Inside the match statement --(A2=$A2:$A$50) returns an array of 0s (if the cell doesn't match A2) or 1s (if it does match). The match statement finds the first 0 in the array (the first number that doesn't match). Since this returns the place in the array that starts at the current row it will give the number of rows that match+1. We just need to subtract that 1 to get the number we need.

Related

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

Using VBA to create a grid from 3 spreadsheets with the row, column, and values

I have three excel spreadsheets. The first has the values that are to be assigned to a new excel spreadsheet. The second has the column that the data belongs in. The third has the row that the data belongs in.
It looks something like this:
Data Value:
1 5 7 9
2 2 6 8
Column Number:
1 2 3 1
2 3 1 2
Row Number:
1 2 3 2
4 4 3 1
How can I combine all of this information to create a single spreadsheet that contains the values in a format like this:
Column
1 2 3
Row
1 1 8 0
2 9 5 0
3 6 0 7
4 0 2 2
I have tried to do it by using loops in vba, but I am a beginner and I am having some difficulty.
I know that I need to use a loop that checks the row and column that the data is supposed to be in against the row and column for each iteration. I am just not sure how to go about doing that.
Assuming these are different sheets in the same workbook (if not - create a new workbook and copy the sheets over). I assume that on each sheet, the same range of cells is used (for example A1:D2 in all 3 cases with the values on the first sheet, the column numbers in the second and the row numbers in the third). You can dispense with VBA entirely at the expense of using some complicate formulas (inspired by this excellent article: http://exceluser.com/blog/1043/how-to-create-two-dimensional-lookups-in-excel-formulas.html )
Step 1. Add a fourth sheet and in A1 add the formula
=CONCATENATE(Sheet3!A1,"_",Sheet2!A1)
and copy it over the appropriate range (e.g. A1:D2). This will give you things like 2_1 which tell you that the corresponding entry in sheet 1 belongs in row 2 column 1. Name this range "location" (formula tab - define names option)
Step 2 - Decide where you want to hold the data (I'm assuming it is in sheet 4 for simplicity) And add the row numbers (1-4) and the column numbers (1-3) as labels. In my case the row labels are in A5:A8 and the column labels in B4:C4 (see the screenshot below). Then in the upper left corner of the values to be filled in (B5 in my case) enter the following formula (suitably adjusted to match your ranges):
=IFERROR(INDIRECT(ADDRESS(SUMPRODUCT(ROW(location)*(location = CONCATENATE($A5, "_",B$4))),SUMPRODUCT(COLUMN(location)*(location = CONCATENATE($A5, "_",B$4))),,,"Sheet1")),0)
and copy it over the intended range. Be careful with the dollar signs - this formula mixes row absolute and column absolute references in an essential way. Somewhat oddly, it actually works:

How to compare a list of rows to another list of rows in Excel?

I am trying to figure out if there are any differences between a list of data with another. In order for a row of data to "match" with another row, the row must have the same values in their corresponding column. The rows themselves do not have to be in any particular order. In particular, I am dealing with a parts list, where there are part numbers, descriptions, etc. I am trying to figure out if any rows of data are different from rows of data from another list.
I found Compare two sheets using arrays, which may have the answer to my problem, but I am having trouble figuring out how to adapt to my code due to inexperience in Visual Basic.
I was able to get it to work for a single column of data, comparing one column of data from one sheet to another, but cannot get it to compare entire rows of data.
Here is an example of I want this to work:
Sheet 1 Sheet 2
Column 1 Column 2 Column 1 Column 2
Row 1 22a 33 11 11
Row 2 22a 33a 22a 33
Row 3 55 22b 55 23b
The code in the link will tell you what is not in sheet 1 but in sheet 2 and vice versa. In this example, I would like the code to tell me Sheet 1 Row 2 and Sheet 1 Row 3 are not in Sheet 2, and Sheet 2 Row 1 and Sheet 2 Row 3 are not in Sheet 1 (Sheet 1 Row 1 and Sheet 2 Row 2 match).
If that is ok by you, you can do it without VBA using the following formula:
={IF(IFERROR(MATCH(A1&"|"&B1;Sheet7!$A$1:$A$3&"|"&Sheet7!$B$1:$B$3;0);-1)=-1;"Unique";"")}
Assuming that each of your tables start in A1 (so that the tables with three entries span A1:B3), and entering this formula into C1 (and copying it down), press CTRL+SHIFT+ENTER when entering the formula to create an array formula, this will show the word "Unique" in column C if the pair in that row on that sheet is not in any of the row-pairs on sheet 2.
You can then use conditional formatting to highlight unique rows, filter on the tables to include only unique rows, or some other way of doing what you need.
NOTE 1: I have entered my numbers in Sheet6 and Sheet7 instead of 1 and 2. The formula written above goes into Sheet6.
NOTE 2: My language use ; instead of , as function separator, so if yours use , you need to change that.
NOTE 3: You will need to expand the ranges Sheet7!$A$1:$A$3 and Sheet7!$B$1:$B$3 if your set grows (this will happen automatically if new rows are inserted in between the old ones). The best is still probably to create named ranges for each of the 4 columns, exchange the references with those, and manage the named ranges instead of the formulas.
NOTE 4: If your data set contains the character "|", you need to change that as well, to match some character that you for sure do not have there.
Alternatively you could in column C on each cheet enter (assuming first entry in C1)
=A1&"|"&B1"
and copy this down, then run the solution from your copied example using that C column instead of on A1 and B1.

A program that will return the cell address of the minimum value in a row?

So I have a chart that looks something like this. Assume that the top left value, 1, is in cell A1:
x= 1 2 3 4 5 6 7 8
4 3 2 1 2 3 4 5
9 8 7 6 7 8 9 10
8 7 6 5 4 3 2 1
Sum= 21 18 15 12 13 14 15 16
There are x values from 1 to 8, and a three columns of values resulting from using it an equation or something below it. The sum is the sum of the three values below their corresponding x-value.
I'm stuck trying to figure something out that will go through the row of sums, find the smallest value, and then assign it's corresponding x-value to a variable. I also need to assign the values to the left and right of that x-value to other variables.
For this particular chart, 12 is the smallest of the sums, so I would assign variable1 = 4, since that is that column's corresponding x-value. Then my second variable, which is called lowerbound, would equal 3, since it is to the left of x = 4, and my third variable, which is called upperbound, would equal 5, since it is to the right of x = 4.
If I could get the cell address returned of the x-value that corresponds to the smallest sum, then I could assign it to a variable, and then simply offset from that cell to assign the other variables. Even if I could make a program that will return me the cell of the minimum sum value, I could offset to the x-row, and go from there.
How would I do something like that?
TL:DR: To ask more clearly, since that's a lot of words: What would a program look like that detects the smallest value in the sum row, and returns the cell address of that value?
The length of the rows are an unknown, and vary a lot, but the length of the columns are given. They do change depending on the problem, but they will always be known. So I will always know how many rows are in a column, but I will not know how many columns are in a row.
This is the most confusingly-worded thing I've ever written in my entire life, but I hope I've explained it well enough to make some sense.
You guys really are amazing, by the way. I've gotten so far on this program, and it's all because of how helpful you are. I honestly think I would still be stuck at the beginning with you guys! You're willing to tolerate a newbie's incessant questions.
I am assuming that the sum is in A4:H4. Please change as applicable
You can use a formula like
=CELL("address",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))
If you want to use VBA then you can use this
Sub Sample()
MsgBox Application.Evaluate("=CELL(""address"",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))")
End Sub
Using your example, the following formula returns the cell address in row 1 whose value in row 5 is the lowest:
=ADDRESS(1,MATCH(MIN(A5:H5),A5:H5,0))
And if you want that cell's value, use INDIRECT. It takes the address as a string.
=INDIRECT(ADDRESS(1,MATCH(MIN(A5:H5),A5:H5,0)))
If you sum the columns by taking the sum of the array. Here is the VBA version:
For j = 1 To 8
For i = 1 To 3
sum(j) = sum(j) + Cells(i + 1, j + 1)
Next i
Cells(5, j + 1) = sum(j)
Next j

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.