Return values from other workbook - vba

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

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.

Compare Excel sheets values to update a third value

Example file So I have two sheets that each have lists of part numbers, plant where they come from and two columns on costs. What I need to do is scan them and if Sheet A and Sheet B both have a row with matching part numbers and the plant they come from, then A's two cost values are updated to match B's costs.
The next step is then to highlight all cells in Sheet A that are not on Sheet B and highlight all cells in Sheet B that were copied to Sheet A. I think this last part can be done at the same time the cell is being copied I'm just not sure how to do any of this.
This is a formula method.
Because you will not be changing all the values and I assume you want to keep those that do not have a match, then in an empty column next to the figures on sheet 1 put the following formula:
=IFERROR(INDEX(Sheet2!F$3:F$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),),0)),G3)
Then copy over one column and down the the end of the data.
The INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),) will create an array of 0 and 1's the same size as the data reference on sheet 2. In this instance it will create a 1 dimensional array that is 5 objects.
The position of these objects of 0 and 1 are relative to the rows. So for the first formula the return array will be {0,1,0,0,0} because only the second row of the data matches both the plant and the part number.
The MATCH(1,INDEX(...),0) then finds the first object in that array that is 1 and returns the relative position, in this case 2 as it is the second in the array.
The Outer INDEX(Sheet2!F$3:F$7,...) then returns the value in the range Sheet2!F$3:F$7 whose relative position is equal to the 2 passed from the MATCH(). So Sheet2!F4.
If no MATCH is found then the whole thing will throw a #N/A error so we capture that error with IFERROR(...,G3) and tell the formula to return the value in column G instead.
This will give you all the proper values:
Then you can copy and paste just the values back to the original spots and hide the columns with the formulas:
Sheet2 for reference:
If you want vba to do the last part of copy and past and hiding then use the macro recorder and then clean up the code.

Dynamic calculation in Excel based on a cell value

I've got a challenge in Excel i hope you guys can solve for me.
I have a drop down list (weeks) where i select the week 2015-18 to 2016-17. (nr 1 in picture)
In the cell called LY (nr 2 in picture) I want that to type the result of a dynamic sum range, based on the weeks input.
Picture of setup of the text above
The calculation logic is:
If i select week 2015-20, the VBA or formula should sum(F5:F7)/(G5:G7).
So in other words, I want a dynamic calculation that starts from week 2015-18 (F5/G5) and then sums the values down to the value that i have selected in "Weeks"
Picture of the setup of the logic values
The value of this calculation should be shown in LY (first picture, nr 2).
I really hope you can solve this for me. I've got more rows to calculate, so if you could come up with a "global" code that works for that, it would be great
ok, so you do not need VBA for this.....
for the sake of easier updates; mark the weekrange, right click them and give them a name, with Define name
I called it yearlyweeks
The two drop downs lists, will contain the value corresponding to the text indicating that week.
So we can use this to get a cells address by value (if that text is unique in the range named yearlyweeks):
=ADDRESS(MATCH(H8,yearlyweeks, 0), 2)
where H8 is the cell address of a dropdownlist cell. 2 is the column index of the "B" column
this will result in something like:
$B$2
where the 2 indicates the relative row index, in the range, not the absolute row number.
we are really not interested in the column, or relative row, only the absolute row, but so far we can live with relative..:
=ROW(INDIRECT(ADDRESS(MATCH(H8,yearlyweeks, 0), 2)))
this will give you the relative row of the cell coresponding to what you selected on the drop downlist for one of the lists. So I would do this in two calculation cells, just to avoid the next piece getting too long..
lets let the calculation cells be in I10, and J10..
I10 hold the relative row for the beginning week
J10 for the ending week
below create a new set of cells with:
="F"&(I10 + 7)
and
="G"&(J10 + 7)
where 7 is the row offset of yearlyweeks
in yet one more cell J12 we make the range string:
=CONCATENATE(I11;":";J11)
and then does the calculation in the final cell:
=SUM(INDIRECT(J12))
Now I can do this as a one liner, but you really would hate that

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.