0

Could you please give me vba code that can solve this problem:

I want remainder in Mod Function can become equal to divisor.

Example: In normal situation Mod(132,12)=0 but I want when remainder is equal to divisor, last step of dividing that is dividing 12 on 12 doesn't do and remainder becomes 12. Example

I wrote this code but it seems something is wrong. What's the problem?

Function XLMod(a, b)

XLMod = Int(a - (b * Int(a / b)))

If XLMod(a / 10, b) = 1 And XLMod(a, 10) = 2 Then

   XLMod = b

End If

End Function
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Hamid M
  • 43
  • 4
  • We're here to help, but would like to see what you have tried on your own. Have tried writing the VBA yourself. Show us what you've got. – Michael Z. Apr 27 '19 at 05:53
  • Look [here](https://stackoverflow.com/q/4378047/10908769) and adapt the formula for your needs. – Asger Apr 27 '19 at 06:08

1 Answers1

0

You need a special exception of the standard modulo function.

If the result of a normal division (a / b) would result in a number ending with 1 (e. g. 1, 31, 10001, 12341, ...), then you want it to return b.

Function XLMod(a, b)
    XLMod = a Mod b
    If XLMod = 0 And (a / b) Mod 10 = 1 Then XLMod = b
End Function
Asger
  • 3,822
  • 3
  • 12
  • 37
  • There is 2 condition that remainder equals to 0: – Hamid M May 01 '19 at 04:29
  • a) 144/12 b) 132/12 There's a difference between this conditions. In second one remainder equals to 12 and in last step we divide it on 12. As I wrote in my code, I need XLMod for this condition but your code is for both of them. – Hamid M May 01 '19 at 04:35
  • I hopefully understood now. If my answer helps you, please mark it as "accepted answer" as described [here](https://stackoverflow.com/help/accepted-answer). – Asger May 01 '19 at 06:45