Add value to another cell until total value reached - vba

I need an excel formula or VBA to solve below problem. So I need you guys to help me please:)
My problem is :
I have a table as blow with given total request and values. What I need is generate new value and the total of those values should give me closest total to "Total Request"
So the generation of new values should stop when it reach >= Total request.
Given Table :
Total Request 35
Value New Value
1
2
3
14
21
12
Requested Table
Total Request 35
Value New Value
1 1
2 2
3 3
14 14
21
12
Thanks in advance :)

IF SUM in expanding range
Presuming you have Value column as column A, you can use this formula and copy down. It should stop on sum=20:
=IF(SUM($A$5:A5)<Total_Request;SUM($A$5:A5);"")
Or if you want just the value as an answer:
=IF(SUM($A$5:A5)<Total_Request;A5;"")

Related

VBA: Offset with cells position

I have been searching, and the Offset only can be fixed (http://www.homeandlearn.org/the_offset_property.html)?
I would like to know how to do a variable offset. For example, I choose a fixed value from worksheet (1), based on the condition that you can see below it will do the offset, and use the offset value in a formula from code of worksheet (2).
Worksheet 1: I have the columns A to H and row 1
A B C D E F G H
1 5 10 15 20
Worksheet 2: I have column B and rows 14 to 18. In this worksheet, the values 2,3,4,5,6 are random and are used to determine the number of columns to offset in the worksheet 1. As follows, I expressed what I am trying to do, may be its easier to understand.
Row B
14 2 Sheets(1).Offset(A1,0,($B2-$B$2)*2,1,1)= Offset(5,0,(2-2)*2,1,1) =5 (A1)
15 3 Sheets(1).Offset(A1,0,($B3-$B$2)*2,1,1)= Offset(5,0,(3-2)*2,1,1) =10 (C1)
16 4 Sheets(1).Offset(A1,0,($B4-$B$2)*2,1,1)= Offset(5,0,(4-2)*2,1,1) =15 (E1)
17 5 Sheets(1).Offset(A1,0,($B5-$B$2)*2,1,1)= Offset(5,0,(5-2)*2,1,1) =20 (G1)
18 6 Sheets(1).Offset(A1,0,($B6-$B$2)*2,1,1)= Offset(5,0,(6-2)*2,1,1) =0 (I1)
During my search, I only found possible to do an offset by placing numbers of rows and columns desired to offset. However, it does not work for me.
Since the following code, returns time error '1004, I would like to know why? Is not supposed to return a value? If everything is defined, why does the offset not working? What's wrong?
Sheets(1).Cells(1,1).Offset(0,(Cells(i+countRows,3) - Cells(i,3))*2))
How can I solve this error?
I hope I made myself clear.
Thanks in advance!

Excel VBA selecting data from a sorted Table

I am running into a problem in VBA in excel.
I am trying to create a participant registration program in excel for a sports tournament. One can add data like the weight, age and name of a participant. And then based on that, The participants are divided into poules named with letters A, B... until Z. I have a table which can sort these poules by letters and then for example only display the participants which are in poule A such as below
Example
Now i want to count the numbers with VBA macros of participants in the poule which is displayed by the sorted table. For example when i sort on poule A it has to return 3 and when i sort on B, it has to return 2.
Determined by the number of participants in a poule the program will print a different poule scheme, depending on the number of participants. (i have this part in VBA)
The problem is when i have a sorted table like below
Example 2
It counts all the participants, and not just the ones in B or any other sorted letter.
I have this:
Sub Count()
Dim nRows As Integer
nRows = Range(Range("A18"), Range("A18").End(xlDown)).Rows.Count
MsgBox (nRows)
End Sub
This works well if you sort A, but when you sort any other letter, it counts All the table until that letter. Eg. when you sort B, it displays 5 (Number of A + B).
I have looked on the internet for a really long time to find a solution but without succes.
I hope my question is clear and that somebody can help me.
I am assuming that you are using worksheet functions. Use Subtotal when working with filtered data.
These parameters evaluate all cells visible or hidden
1 AVERAGE
2 COUNT
3 COUNTA
4 MAX
5 MIN
6 PRODUCT
7 STDEV
8 STDEVP
9 SUM
10 VAR
11 VARP
These parameters evaluate only visible cells
101 AVERAGE
102 COUNT
103 COUNTA
104 MAX
105 MIN
106 PRODUCT
107 STDEV
108 STDEVP
109 SUM
110 VAR
111 VARP
The code does work now, except that it only counts the first letters it encounters.
So when the first column for the poules is for example A A A E A A B B E.
And i sort to A and use the count function, it only returns a value of 3 and not of 5 (because there are 5 A's)
When I sort the table to A, it looks like this (column number, poule value):
14 A
15 A
16 A
18 A
19 A
And it returns just a count of 3, have you maybe got any fixes for that problem as well?
Pictures:
sorted tabel to E
Table
Range.SpecialCells will return a range of only visible cells.
Dim rSource As Range
Dim rVisibleCells
Set rSource = Range(Range("A2"), Range("A2").End(xlDown))
Set rVisibleCells = rSource.SpecialCells(xlCellTypeVisible)
MsgBox rVisibleCells.Rows.Count

Excel Macro find and copy corresponding cell apply formula on the value

Hi I am new to VBA script. I have the following problem.
Sheet 1
Date Sum of cnt
01-04-2014 77
01-06-2014 3
01-07-2014 2
01-08-2014 1
01-09-2014 921
Sheet 2
Date count (count/sumofcout(sheet1)
01-06-2014 3
01-09-2014 4
01-09-2014 712
01-07-2014 1
01-08-2014 1
01-09-2014 205 .....
I have to search for the every single date from sheet 1 if there is match in sheet 2 , corresponding count(sheet2)/sum0fcount of that date(sheet1) should be my third column in sheet 2. Please anyone help me. I have large data multiple values of each day in a month.
Thanks in advance.
As Tim has suggested in his comment above...you can use Vlookup/Match (along with SumIF) to get your results. A formula like this (with different ranges) should do the job:
IF( ISNA(MATCH(A2,Sheet1!$A$2:$A$13,0)),"",B2/SUMIF(Sheet1!$A$2:$A$13,A2,Sheet1!$B$2:$B$13))
You can change the Ranges A2:A13, B2:B13 to adjust this according to your data

Get the max value of un-hidden cells

I have a table that has 20 rows and the table un-hides the amount of rows specified in the cell above my table. so if I put in 5 into the cell, 5 of the 20 rows will be un-hidden.
So now I would like to get the MAX of the say 5 cells that are now unhidden. How would I do this?
=SUBTOTAL(104,RANGE)
104 will give you the max of the unhidden cells. If you replace 104 with 4 it will operate as the normal MAX function UNLESS you are filtering the rows using the built in Excel table function.
Perhaps
=SUBTOTAL(104,your_range)

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