add the same number until it exceeds another and return that number vba

Sotos13

New Member
Joined
Mar 8, 2019
Messages
42
Hello everyone

I'm new in vba and try to figure out some stuff. I have a two columns in excel and i want to compare each number , for example A1 with B1. A2 with B2 etc..
If A1 is smaller than B1 return in cell C1 the B1. If not add the number in B1 as many times until it is bigger than A1.

[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl66, width: 64, align: right"]1[/TD]
[TD="class: xl65, width: 64, align: right"]8[/TD]
[TD="width: 64, align: right"]10[/TD]
[/TR]
[TR]
[TD="class: xl66, align: right"]2[/TD]
[TD="class: xl65, align: right"]45[/TD]
[TD="align: right"]25[/TD]
[/TR]
[TR]
[TD="class: xl66, align: right"]3[/TD]
[TD="class: xl65, align: right"]14[/TD]
[TD="align: right"]4[/TD]
[/TR]
</tbody>[/TABLE]
in line 1 the answer is 8<10 = 10, line 2 the answer is 25+25 = 50,line 3 the answer is 4+4+4+4 = 16.

So i started like this but i cant figure out how to proceed..
Dim qAnswer As Integer
qAnswer = 2
If Cells(qAnswer, 1).Value < Cells(qAnswer, 2).Value And Not IsEmpty(Cells(qAnswer, 1).Value) Then
Cells(qAnswer, 3) = Cells(qAnswer, 2).Value
Else

Any suggestions?
Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
i will probably post some more within the next days because it's not that easy for me now in the beginning. i hope you'll see them.
thanks again
 
Upvote 0
Sounds like you have headers above your values. So far you've said your values start from A1.

There's an easy fix for this: Set the Rng to start from cells(2,1)
 
Upvote 0
If anyone is interested in a solution with formulas, maybe


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
8​
[/td][td]
10​
[/td][td]
10​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
45​
[/td][td]
25​
[/td][td]
50​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
14​
[/td][td]
8​
[/td][td]
16​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
34​
[/td][td]
2​
[/td][td]
36​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
16​
[/td][td]
12​
[/td][td]
24​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
25​
[/td][td]
16​
[/td][td]
32​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
22​
[/td][td]
10​
[/td][td]
30​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
8
[/td][td]
36​
[/td][td]
4​
[/td][td]
40​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
9
[/td][td]
6​
[/td][td]
12​
[/td][td]
12​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
10
[/td][td]
48​
[/td][td]
8​
[/td][td]
56​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
11
[/td][td]
50​
[/td][td]
2​
[/td][td]
52​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
12
[/td][td]
10​
[/td][td]
8​
[/td][td]
16​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
13
[/td][td]
20​
[/td][td]
10​
[/td][td]
30​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
14
[/td][td]
11​
[/td][td]
6​
[/td][td]
12​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
15
[/td][td]
48​
[/td][td]
10​
[/td][td]
50​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
16
[/td][td]
16​
[/td][td]
5​
[/td][td]
20​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
17
[/td][td]
17​
[/td][td]
8​
[/td][td]
24​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
18
[/td][td]
7​
[/td][td]
18​
[/td][td]
18​
[/td][/tr]
[/table]


Formula in C1 copied down
=IF(B1>A1,B1,CEILING(A1+1,B1))

M.
 
Upvote 0
thank you Marcello..It works also like this!!!
but for some numbers it returns a higher number for example 50 and 2 and returns 52 not 50
 
Last edited:
Upvote 0
thank you Marcello..It works also like this!!!
but for some numbers it returns a higher number for example 50 and 2 and returns 52 not 50

If A1 is smaller than B1 return in cell C1 the B1. If not add the number in B1 as many times until it is bigger than A1.

I thought you wanted the result always greater than column A, not greater or equal.

If column C equal to column A is acceptable, then change the formula to C1 for simply ..
=IF(B1>A1,B1,CEILING(A1,B1))
and copy down

M.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top