How to Filter out Repeating Elements in an Array

Seba5404

New Member
Joined
Sep 29, 2015
Messages
10
Hey Guys,


So I am basically done with the program. I've gotten the results I wanted mostly. My objective is to put down 20 cards and make sure there is no repeating Cards. This is my Code so far. All I'm having trouble with is that I cannot seem to figure out on how to make a filter for those values.
Any advice or examples would be greatly appreciated. Thanks in Advance!
Code:

Option Explicit
Dim MainFileNameLocation As String
Dim MainFileName As String
Sub Cardgame()


Dim CardRange As Range 'Ranges of cells that we will use to place the cards within
Dim Suits() As Variant 'Array to hold suit types
Dim CardCounter As Long
Dim Rsuite As Variant
Dim x As Integer
Dim Deck() As Variant

'(DEBUG CODE - BEGIN): Make sure to delete this an re-activate the MainFileName line when done
MainFileNameLocation = "C:\Users\sebastian.zdarowski\Desktop\VBA Files\Poker game_1_Copy"
MainFileName = "Poker game_1_copy"
Workbooks.Open (MainFileNameLocation)
'(DEBUG CODE - END)


'Loop through the range and place card values with selected range
x = 13
Worksheets("Cards").Activate
Restart:
For Each CardRange In Range("b2", Range("e6"))
If CardRange = "" Then
Rsuite = Int(Math.Rnd * 4) + 1
If Rsuite = 1 Then
Rsuite = Sheets("symbols").Range("b1").Value 'Hearts
ElseIf Rsuite = 2 Then
Rsuite = Sheets("symbols").Range("b2").Value 'Diamonds
ElseIf Rsuite = 3 Then
Rsuite = Sheets("symbols").Range("b3").Value 'Spades
Else
Rsuite = Sheets("symbols").Range("b4").Value 'Clubs
End If

CardRange = RandomCards(x) & Rsuite
CardCounter = CardCounter + 1
ReDim Preserve Deck(1 To CardCounter)
Deck(CardCounter) = CardRange
End If
Next CardRange


DidYouWin: MsgBox "Have you won? How well did you do?"

End Sub

Public Function RandomCards(x)
Dim Rcards As Variant


'Function used to get Random cards dealt to the deck
RandomCards = Int(Math.Rnd * x) + 1
If RandomCards = 11 Then
RandomCards = "J"
ElseIf RandomCards = 12 Then
RandomCards = "Q"
ElseIf RandomCards = 13 Then
RandomCards = "K"
ElseIf RandomCards = 1 Then
RandomCards = "A"
Else
Rcards = Rcards
End If

End Function

EXCEL 2013
Windows 7
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

I find it easier to do this type of thing the other way round.

Write all 52 cards to a column.
In the next column add a random number.
Sort both columns by the random number in the second column.
Read as many cards as you need from the top of the list.

That way you will never get any duplicates.
See if you can adapt this:
Code:
Sub Shuffle()

    Dim ws As Worksheet
    Dim i As Long
    Dim Suits As Variant

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Suits = Array("S", "H", "D", "C")
    
    Randomize
    With ws
        .Cells.Clear
        .Range("A1:B1").Value = Array("Card", "N")
        For i = 1 To 52
            .Cells(i + 1, "A").Value = ((i - 1) Mod 13 + 1) & Suits(Int((i - 1) / 13) + LBound(Suits))
            .Cells(i + 1, "B").Value = Rnd
        Next
        .Range("A1:B53").Sort Key1:="N", Header:=xlYes
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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