View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] paul.robinson@it-tallaght.ie is offline
external usenet poster
 
Posts: 789
Default Displaying possible combinations

Hi
Try this.

Sub poker_hand2()


Dim card1 As Integer, card2 As Integer, card3 As Integer, card4 As
Integer, card5 As Integer
Application.ScreenUpdating = False

For card1 = 1 To 52
For card2 = card1 To 52
For card3 = card2 To 52
For card4 = card3 To 52
For card5 = card4 To 52
If Not (card1 = card2 And card2 = card3 And
card3 = card4 And card4 = card5) Then
ActiveCell = Card(card1) & "-" &
Card(card2) & "-" & Card(card3) & "-" & Card(card4) & "-" &
Card(card5)
If ActiveCell.Row = 65536 Then
ActiveCell.Offset(-65535, 1).Select
Else
ActiveCell.Offset(1, 0).Select
End If
End If
Next card5
Next card4
Next card3
Next card2
Next card1


End Sub

Function Card(x As Integer) As String
Select Case x
Case Is <= 13: Card = x & "D"
Case Is <= 26: Card = x & "H"
Case Is <= 39: Card = x & "S"
Case Else: Card = x & "C"
End Select
End Function

As usual, careful with the line wrapping. Also note that
Dim card 1, card2 as integer

makes Card2 an integer but Card1 a variant. What you mean is
Dim card 1 as integer, card2 as integer

If you don't do this, you will get an error calling the function Card.
regards
Paul

On Jun 23, 8:11*am, James8309 wrote:
Hi everyone,

I was researching on how to display possible poker hands (5 cards)
using a VBA. I found this code within this group. However as you can
see, this code below does ignore the suits. Is it possible to alter
this code to display different possible poker hands including the
suits?

Thank you very much.

where;
1. Diamond = "D"
2. Heart = "H"
3. Spade = "S"
4. Clover = "C"
'Ace' being = 1, 'King' being = 13

Sub poker_hand2()

Dim card1, card2, card3, card4, card5 As Integer

* * For card1 = 1 To 13
* * * * For card2 = card1 To 13
* * * * * * For card3 = card2 To 13
* * * * * * * * For card4 = card3 To 13
* * * * * * * * * * For card5 = card4 To 13
* * * * * * * * * * * * If Not (card1 = card2 And card2 = card3 And
card3 =
card4 And card4 = card5) Then
* * * * * * * * * * * * * * ActiveCell = card1 & "-" & card2 & "-" &
card3 &
"-" & card4 & "-" & card5
* * * * * * * * * * * * * * If ActiveCell.Row = 65536 Then
* * * * * * * * * * * * * * * * ActiveCell.Offset(-65535, 1).Select
* * * * * * * * * * * * * * Else
* * * * * * * * * * * * * * * * ActiveCell.Offset(1, 0).Select
* * * * * * * * * * * * * * End If
* * * * * * * * * * * * End If
* * * * * * * * * * Next card5
* * * * * * * * Next card4
* * * * * * Next card3
* * * * Next card2
* * Next card1

End Sub