Solved Find the next first devisable number

KitaYama

Well-known member
Local time
Tomorrow, 03:51
Joined
Jan 6, 2022
Messages
1,906
It's more a math question rather than vba.
But we don't have math forum, and there are a lot of members who know math better than me.

I need to find the smallest number that is greater than a given number and divisible by a variable.

Two examples:

CountOfRows=8
CurrentCount= 51
What is the first number that is greater than 51 and is divisable by 8? (NextCount=56)

CountOfRows=10
CurrentCount= 181
What is the first number that is greater than 181 and is divisable by 10? (NextCount=190)

I know I can use
NextCount = CountOfRows * (Roundup(CurrentCount/CountOfRows))

But I can not use RoundUp.
Int doesn't return correct result.

I can use mod. But I couldn't find a way around it.
Any suggestions?
Do you have a better way?
thanks
 
Last edited:
Just adding CountOfRows & CurrentCount will always be integer. No floating point at all. Never.
thanks again.
 
Last edited:
Or you could do: Find Z as next largest integer than X but divisible by Y, where X, Y, and Z are all positive integers -

Code:
DIM X AS LONG, Y AS LONG, Z AS LONG
...
Z = Y * ( ( X \ Y ) +1 )
...

If you weren't familiar, "\" is VBA's "forced integer divide" operator. Also no floating point.
 
NextCount = CurrentCount + (((CountOfRows - (CurrentCount MOD CountOfRows)) MOD CountOfRows)

edit--the above finds the number greater than or equal to CurrentCount that is divisible by CountOfRows

below will find number greater than CurrentCount divisible by CountofRows


NextCount = CurrentCount + (CountOfRows - (CurrentCount MOD CountOfRows))
 
Last edited:
If you weren't familiar, "\" is VBA's "forced integer divide" operator. Also no floating point
Now that you brought it up, I remember reading about it a million years ago, but since I've never used it, had completely forgotten. Thanks for reviving it.
But as I said, unfortunately it's not a VBA problem. I'm programming a robot that comes with a Mitsubishi Lazer Cut machine. I'm limited to NC language and G-Code.
Thanks any way for trying to help.

@plog, you're a star. I simply don't know to thank you. Even though your second formula is the answer to my question, now that I think more carefully about the situation, I think I will go with the first one.
Million thanks.

PS: I'm still running some tests. If I faced any problem, I'll be back.
 

Users who are viewing this thread

Back
Top Bottom