VBA Code to sum values in one column based on the value in another column in excel - excel

I am a VBA Newbie and a first timer on this forum - I have followed all the instructions on the "how to ask" page - I may have missed something please do advise
I am creating an EV report for project tracking based on the following column headers (showing only a few for brevity)
"TaskName" "Status" "BaselineStart" "BaselineFinish" "BaselineEffort"
need to sum up the values in the BaselineEffort column in 7 day increments after checking if the value in the BaselineFinish column is less than or equal to the 7th day value
I believe the answer lies in using arrays, but need handholding with that concept to understand how it works
Pivot and Excel formulas dont work for me because the table is dynamic while the report is static and I need to remove user intervention in creating the report
Thanks in advance

say that the criteria column starts in A1 and the colume to be summed starts in B1
do while not isempty(range("a1").offset(x,0))
if range("a1").offset(x,0) = SomeValue then
sum = sum + range("a1").offset(x,0)
end if
x = x + 1
loop
this code will run until it has looked at each item in column A, and add the value in column B to a sum I called "sum" if the value in column A equals "SomeValue." I doubt you can actually use the variable name "sum."
I hope that's useful.

Related

Excel - How to get the previous value on a column with an if statement?

I am trying to automatically pair Issue ID's (Column I) to their parents filling the Column J.
My structure is:
Epic
- Story
-- Technical task
Stories need to be parented to Epics, and Technical tasks need to be parented to Stories.
I need a formula that:
For Stories: Search on Column C for the previous "Epic" and fill the specific "Story" cell on Column J with the value of that "Epics" from Column I.
For Technical tasks: Search on Column C for the previous "Story" and fill the specific "Technical task" cell on Column J with the value of that "Story" from Column I.
What I have:
Expected result:
You can use this formual in J - if you have Excel 365:
=IF(C2<>"Epic",
LET(lookFor,IF(C2="Technical task","Story","Epic"),
f,FILTER($I$2:$I2,$C$2:$C2=lookFor),
INDEX(f,COUNTA(f))),"")
lookfor returns the parent issue type
then the ID column is filtered to only return the according issue types IDs above the current row
then the last of the returned IDs is given
I don't understand your question (I have the idea that you have by mistake uploaded two times the same screenshot), but as far as I understand, you might need the Offset() worksheet function, which allows you to get values from other cells in other rows or columns, as in following example:
=OFFSET(D2,0,-2)
This takes the value, starting from cell "D2", stay on the same row but take two columns to the left. The result equals the value of cell "B2".
If you have Xlookup, you can just do a reverse search:
=IF(C2="Epic","",XLOOKUP(IF(C2="Technical task","Story","Epic"),$C$2:$C2,$I$2:$I2,"Not found",0,-1))

How to count multiple text in multiple columns

I am Working off a large table in Excel and I want to sort the data into categories. What I’m trying to do is get Excel to count how many times a criteria in column C, D, & E occurs and returns the value. So look through C:C pick “Company”, then look through D:D for “Full Time – Temp” and then E:E for a location such as “Factory”. See link to sample table below.
Example:
G4 =COUNTIFS($C:$C,"company",$D:$D,"full time - temp",$E:$E,"home") and it returns 0
I4 =COUNTIFS($C:$C,"company bilingual",$D:$D,"bilingual - FT - perm") and it will return 3
My problem is column E
If I wanted to return in cell J4 how many “Company Bilingual” are “Bilingual - FT – Perm” and located in “Factory” I get 0.
I’ve tried using
J4 =COUNTIFS($C:$C,"company bilingual",$D:$D,"bilingual - FT – perm",$E:$E,"Factory") but it returns 0 and what I want it to return is 2, which I understand it is saying there is no Factory cell on its own, all the cells that have Factory have 3 items in them e.g Factory - Dallas. So I want to count all the factories in column E but not where the factory is actually located.
In summary what I want to do is find a function or array that will count one unique occurrence in column C, D, and E. If a cell in a column has more than one word I would like to be able to pick one word and ultimately still count all occurrences in the other columns and return a value.
In my research I have come across different suggestions but none that helps my problem.
I hope I've explained my problem, any assistance is greatly appreciated.
Screenshot of table
I suggest you make the criteria table and mention your criteria there as shown by me in the snapshot.Giving due credit to #Harsha Vardhan, his suggestion is correct approach as given in his comments. I have made a fully working example for clear understanding.
For partial string match I used a concatenated string in I2 ="*"&"Factory"&"*"
Criteria Table is in the Range G1:I4 and Results are in the Range J1:J4
Formula to be entered in J2 to J4 respectively are as per criteria mentioned in the table.
=COUNTIFS($C:$C,$G$2,$D:$D,$H$2,$E:$E,$I$2)
=COUNTIFS($C:$C,$G$3,$D:$D,$H$3)
=COUNTIFS($C:$C,$G$4,$D:$D,$H$4,$E:$E,$I$4)
Results are as per your requirement as shown in the snapshot.
EDIT DATE 23-06-2016
This has reference to OP's comments on 22nd and 23rd June 2016. There is no change in the formulas. It is required that conditions are put correctly in the criteria table. As per new criterion specified by OP, following snapshot shows that correct results are obtained. Further file count multiple text 23062016 has been uploaded for perusal.

Incrementing a Cell in Excel which is associated with different columns and formulas and I wish to record the results of each increment carried out

I am looking to find a solution for the following:
I am trying to obtain the maximum revenue that can be earned by incrementing a cell in this sprenter image description hereeadsheet below, recording the results obtained from each increment and simply finding the maximum value of the list of recorded values from incrementing.
I am focusing on the cell J4 (Rev if New floor set) which contains the following formula [=SUMIF(B4:B24,">"&I4,H4:H24)] which computes a sum of the values in the column ‘Seller receives after Rev Share if floor increased’ that correspond to the rows where ‘Buyer_Bid > Minimum to be paid by Buyer’.
The Cell ‘Minimum to be paid by Buyer’(&I4) contains the formula [=F4/0.84] where the column F contains the Floor_Price. to be set (What I want to increment to find maximum solution (Overall Revenue).
The Cells in the column ‘Seller receives after Rev Share if floor increased’ contain the following formula ‘[=IF(E4>F4,E4,G4)] which places in the instance where the rows in column E ( Seller receives after Rev share) > rows in column F (New_Floorprice) the corresponding rows where this statement is true from column E and places them in Column H and for the instance that this condition fails it puts the corresponding to the rows failing the condition from Column G into H.
The Cells in the column ‘Buyer_Bid’ simply contain values which were imported and not created within the spreadsheet.
The columns which make up the final figure ‘Rev if new floor set’ depend on the New_Floorprice which I want to increment to find out which floor price results in the most revenue(Overall Revenue = Rev if New Floor Set – Rev Today)
I was hoping to be able to automatically increment Cell B1 by increments of 0.001 which then would change all the corresponding values in the spreadsheet as I have the Column F = B1 and then record the results for ‘Rev if New Floor Set’, ‘Rev Today’ and ‘Overall Rev’ for each increment carried out so I can simply obtain the optimal Floor price that produces maximum revenue. However 'Rev Today' solution will stay constant as the column that the formula is made up of was imported and left as is.
I have tried using solver for this problem by setting the objective to column L4 and choosing to maximise changing the variable cell B1 with the constraint of L4>=0 however kept getting a different result when I manually changed to optimal floor price B1.
I am trying to make this as clear as possible but please let me know if it is hard to understand. Also I have never used VBA so a solution which doesn't involve VBA would be great please. I have a link to an image of the spreadsheet below.
It would be much appreciated if I Could get some help with this please.
Many thanks,

Combining VLOOKUP with SUM

I am trying to VLOOKUP column say "B" values from "J" but there are duplicates in B. How can I sum those values in J for same B reference in a lookup formula without doing pivot, etc? As this is a report from a third party and needs to be updated around 10 times a day, doing other steps is very uncomfortable.
So a normal formula bring me first value against first unique reference in B and so on. I would like this to give me sum of all values in J for first reference and so on.
PS: I couldn't post image which would have helped us all.
You are probably looking for the Sumif formula:
=SUMIF(B:B;valueYouWant;J:J)
And if you want to show "NO VALUE" when the value is not found, add a VLOOKUP too. Suppose your search data is in columns A and B of the slave sheet:
=IF(ISNA(VLOOKUP(valueYouWant; Slave!A:B; 2; false)); "NO VALUE"; sumif(Slave!A:A;valueYouWant;Slave!B:B))

"Resetting" Average in Excel

Please pardon my poor explanation of my problem, any insight would be greatly appreciated.
I am using an average in excel that begins at a fixed cell and fills down.
For example: column B contains values, and column C contains the formula =AVERAGE($B$1:B1) filled down, so at row 10 the formula is =AVERAGE($B$1:B10)
I am wondering if there is a way to calculate this average that will "reset" when the value in the column A changes. Getting a formula for whether column A changes is easy using an IF function but I don't know how to change the reference cell when the average "resets." I attempted to attach an image with the formulas but I do not have 10 reputation so here is a link to a short example sheet:
https://drive.google.com/file/d/0BymZUcneHsYgUnRoYkRkdnJYbmM/view?usp=sharing
I would prefer do accomplish this without VBA if possible.
Thank you very much.
EDIT: would it be possible to have a count function next to the data column that starts over when column A switches from "A" to "B" (or "B" to "A"), then I could simply use an average offset with the resulting value to get the desired average?
You should use the =AVERAGEIF function.
=AVERAGEIF($A$1:A1,A1,$B$1:B1)
Here's how I ended up solving this:
I used =ROW()-MAX(INDEX((A$1:A2<>A2)*ROW(A$1:A2),0 to count down until "A" changed to "B" or vice versa, then used =ROW()-MAX(INDEX((A$1:A2<>A2)*ROW(A$1:A2),0 to average the data with the offset calculated in the previous formula.
Note that the data was still in column B, with "A" or "B" in column A, and these two formulas were placed in I and J, respectively
Thank you very much for all your help
You can use Indirect to resolve a custom range. Since values in "A" can come multiple time in distinct block, you will have to add an extra column to keep track of the starting row of each range :
(starting from row 2, [C1] = 1)
=IF(A2<>A1,ROW(A2),C1)
the result will be in column "D" :
=AVERAGE(INDIRECT("B"& C1 &":B"&ROW(A1)))

Resources