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.

1810
24525
3144

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
try

Code:
Sub sotos()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row in column A
For x = 1 To lr 'loop though rows
    If Cells(x, 1) < Cells(x, 2) Then
        Cells(x, 3) = Cells(x, 2)
    Else
        'loop until colum B is > column A
        i = Cells(x, 2)
        Do Until i > Cells(x, 1)
            i = i + Cells(x, 2)
        Loop
        Cells(x, 3) = i
    End If
Next x

End Sub
 
Upvote 0
Welcome to the message board!

Here's how I'd solve this:
Code:
Sub MultiplyValues()

Dim c As Range
Dim Rng As Range


With ActiveSheet
'Set the range to calculate:
    Set Rng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    
    For Each c In Rng
        With c
'Makes sure the number to be multiplied is greater than 0:
            If .Offset(, 1).Value < 0 Then
 
                .Offset(, 2).Value = Application.WorksheetFunction.RoundUp(.Value / .Offset(, 1).Value, 0) * .Offset(, 1).Value
            
            End If
        End With
    Next c
    
End With


End Sub
My code skips a couple of corners but should return the desired output as long as the second number is an integer greater than 0.
 
Upvote 0
Noticed a tiny bug in my code: I used "smalled than" instead of "greater than"

Also came to think that my code will not work correctly with negative numbers and decimals.
 
Upvote 0
Hi all
thanks for the quick response.
the thing is that Scott's code makes excel not responding and Misca doesn't do anything. I change it to > but no luck.
By reading your Scott it makes sense but it doesnt seem to work.
 
Upvote 0
Do you have blanks in column B or negative numbers? If so how should the be handled?
 
Upvote 0
if i split the code, the first part works fine (see below) but with all the code it doesnt respond. Does it get into an endless loop?
Dim lr As Long
Dim i As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row in column A
For x = 1 To lr 'loop though rows
If Cells(x, 1) < Cells(x, 2) Then
Cells(x, 3) = Cells(x, 2)

End If
Next x

aaabbbccc
81010
4525
148
342
1612
2516
2210
364
61212
488
502
108
2010
116
4810
165
178
71818

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
if i split your code Scott, it works great. but by adding the second part (Else loop until colum B is > column A) it doesnt respond.
it's like an endless loop or not?
No i dont have blank or negative.
 
Upvote 0
The code works for me. How many rows do you have? The more rows the longer it will take. When I have 77826 rows then it briefly says not responding while the code runs but it does run.
 
Upvote 0
If this your table it looks like your numbers are formatted as text.

The code in my macro checks the value is greater than zero. The value of text is always zero. Try this instead:
Code:
Sub MultiplyValues()

Dim c As Range
Dim Rng As Range




With ActiveSheet
'Set the range to calculate:
    Set Rng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    
    For Each c In Rng
        With c
'Makes sure the number to be multiplied is greater than 0:
            If --.Offset(, 1).Value > 0 Then
 
                .Offset(, 2).Value = Application.WorksheetFunction.RoundUp(.Value / .Offset(, 1).Value, 0) * .Offset(, 1).Value
            
            End If
        End With
    Next c
    
End With




End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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