VBA - Logic Problem

L

Legacy 143009

Guest
Hi,

I am stuck while making an If statement. I have two playing cards. Let's say card1 and card2. Suits have no significance.
If I add between 1 and 6 to the card2's value and can still reach card1's value, then card1 will go to slot1 and card2 will go to slot2.
If vice-versa card1 will go to slot2 and card1 will go to slot1.
I wrote some code but works buggy.

Some examples:
9 and 6 → 6 will be in slot2 because If I add 3 to 6, I can reach to 9.
3 and 12 → 12 will be in slot2 because if I add 4 to12, I can reach to 3. (yes there is a modulus 13 thing because they are playing cards)
4 and 10 → 4 will be in slot2 because if I add 6 to4, I can reach to 10.


VBA Code:
        If ((card2+ 5) Mod 13) + 1 >= card1 Or card1 <= card2 Then
          slot1 = card1
          slot2 = card2
        Else
          slot1 = card2
          slot2 = card1
        End If
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
@Flashbond I know you are wanting vba but initially, can you confirm whether results from the below are non-buggy ?

MayMrXL.xlsm
ABCDEFGHIJ
1123456
2Card14
3Card21011120123
4Slot1Card2
5Slot2Card1
6
Sheet12
Cell Formulas
RangeFormula
E3:J3E3=MOD($B3+E1,13)
B4B4=IF(COUNTIF(E3:J3,B2)=1,A2,A3)
B5B5=IF(COUNTIF(E3:J3,B2)=1,A3,A2)
 
Upvote 0
To clearify one point:
There is number 13 and no 0. After 13 it revolves to 1.
 
Upvote 0
So the above is aok for 1, 13 and 13,1 scenario?
 
Upvote 0
If yes then I am thinking your code will be along these lines.

VBA Code:
slot1 = card2
slot2 = card1
For n = 1 To 6
    If Evaluate("=MOD(" & card2 + n & ",13)") = card1 Then
        slot1 = card1
        slot2 = card2
        Exit For
End If
Next n
 
Upvote 0
Solution
Yes,

I modified this formula according to my need and it still works:
Excel Formula:
=MOD($B3+E1-1,13)+1
 
Upvote 0
Then make this edit to the above code?

VBA Code:
If Evaluate("=MOD(" & card2 + n - 1 & ",13)") + 1 = card1 Then
Hope that helps.
 
Upvote 0
Thanks for your work!

I will mark your post as answer yet it is the only solution I have got.
But I am not happy with the iteration :)
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,309
Members
449,095
Latest member
Chestertim

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