Converting lengthy Excel function to SQL? - sql

IFERROR((SUMIFS('Sheet 1'!$K:$K,'Sheet 1'!$A:$A,'Sheet 2'!I$5,'Sheet 1'!$C:$C,'Sheet 2'!$B15,'Sheet 1'!$K:$K,"<>0"))/(SUMIFS('Sheet 1'!$J:$J,'Sheet 1'!$A:$A,'Sheet 2'!I$5,'Sheet 1'!$C:$C,'Sheet 2'!$B15,'Sheet 1'!$K:$K,"<>0")),"")
I am working with the following function in Excel and need to interpret it into MS SQL. I am familiar with SQL, but extremely unfamiliar with excel. From what I understand the function is returning "" if error. otherwise it is calling SUMIF on the innermost parentheses - inside of which I am not sure what is going on, even after looking up what !, :, and $ do in Excel

Okay since I have no idea what you sheet is I can't help with the SQL but i will try and break down the function for you
#this part is for if the enclosed returns an error like #VALUE
#you can think of this as a try rescue block of sorts
#so if there is an Error then Return ""
IFERROR(
(
#This part is Summing All the values in Column K for multiple criteria
#Sum all the values in Column K ref ['Sheet 1'!$K:$K]
#Where all the values in Column A = Value in Cell I5 ref['Sheet 1'!$A:$A,'Sheet 2'!I$5]
#And Values in Column C = Value in Cell B15 ref [ 'Sheet 1'!$C:$C,'Sheet 2'!$B15]
#And the Values in Column K Do not = 0 ref ['Sheet 1'!$K:$K,"<>0"]
SUMIFS('Sheet 1'!$K:$K,'Sheet 1'!$A:$A,'Sheet 2'!I$5,'Sheet 1'!$C:$C,'Sheet 2'!$B15,'Sheet 1'!$K:$K,"<>0")
#Above Number Divided By
)/(
#This part is Summing All the values in Column J for multiple criteria
#Sum all the values in Column J ref ['Sheet 1'!$J:$J]
#Where all the values in Column A = Value in Cell I5 ref['Sheet 1'!$A:$A,'Sheet 2'!I$5]
#And Values in Column C = Value in Cell B15 ref [ 'Sheet 1'!$C:$C,'Sheet 2'!$B15]
#And the Values in Column K Do not = 0 ref ['Sheet 1'!$K:$K,"<>0"]
SUMIFS('Sheet 1'!$J:$J,'Sheet 1'!$A:$A,'Sheet 2'!I$5,'Sheet 1'!$C:$C,'Sheet 2'!$B15,'Sheet 1'!$K:$K,"<>0")
)
,"")
Here is the definition of SUMIFS. Quick Synopsis:
*First Argument is the Rows being Summed
*Second Argument is the Criteria Being Evaulated
*Third Argument is the Expression Being Evaluated Against
*Repeat Second and Third for all additional Criterium
Hacked SQL
SELECT Sum(Sheet1.ColumnK) / Sum(Sheet2.ColumnJ)
FROM Sheet1 JOIN Sheet2
WHERE
Sheet1.ColumnA = 10 --I Used 10 in place of Sheet2.ColumnI Row5 as this does not translate directly in SQL
AND Sheet1.ColumnC = 20 -- Same As Above Substitution for Sheet2.ColumnB Row 15
AND Sheet1.ColumnK <> 0
Hope this helps

SUMIFS() adds a range of cells based on one or more criteria applied to a range of cells.
First is the range of values to be summed up, then after that come pairs of criteria, first is the criteria range, then the criteria. The first one breaks down something like this:
SUMIFS('Sheet 1'!$K:$K -- Sum this field
,'Sheet 1'!$A:$A,'Sheet 2'!I$5 --When same row in A matches Sheet 2 I5
,'Sheet 1'!$C:$C,'Sheet 2'!$B15 --When same row in C matches Sheet 2 B, but 15 rows down.
,'Sheet 1'!$K:$K,"<>0") --When the values aren't 0
That's the numerator in your formula, you can break down the 2nd SUMIF() similarly.

Related

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,

Sum function referenced to column number VBA

I need to reference columns by number in Sum function (i.e. Column B =2, C=3, E=5, etc)
i tried this code but it didn't work (ColN is the column number read from a listbox index)
Range("M5") = WorksheetFunction.Sum(Range(ColN& "2" : ColN&"5"))) '(F2:F5) for example
I prefer also to use formulaR1C1 cos this function needs to be applied for the rest of the rows (i.e the next cell would be the sum of F3:F6 and so on)
All help to solve the problem would be appreciated.
You can do it like this:
Range("M5") = WorksheetFunction.Sum(Range(Cells(2, 6), Cells(5, 6)))
The Cells functions accepts a row and column parameter, which is similar to what you want for R1C1 notation. In this case, 6 means column 6, i.e. column F. In your case, you have colN as a column number, so you could do this:
'Dim colN As Long
'colN = 6 ' read from a listbox index in your case, for example
Range("M5") = WorksheetFunction.Sum(Range(Cells(2, colN), Cells(5, colN)))

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.