Need help comparing two ranges of multiple cells to see if each cell in the ranges are the same

TIMOTHYs

New Member
Joined
Jan 15, 2019
Messages
3
Basically I am trying to build a cheesy slot machine simulator lol. I am having trouble figuring out the code that will compare the range ("B2:D2") to("B3:D3") and then compare ("B3:D3") to ("B4:D4") and finally ("B2:D2") to ("B4:D4"). If any are the same then they win. I have tried:

dim bl as range, cl as range, dl as range, ai as range, Winner as Integer <-- this is dependent on the amount of the bet

set bl = activesheet.range("B2:D2")
set cl = activesheet.range("B3:D3")
set dl = activesheet.range("B4:D4")

if bl = cl then ai.value = ai.value + (Winner*2)<---Here I get the error object variable or with block variable not set
if cl = dl then ai.value = ai.value + (Winner*2)
if dl = bl then ai.value = ai.value + (Winner*2)

I also tried:
for each cell in activesheet.range("b2:b3")
if cell.value = 1 then j = 1
if cell.value = 2 then k = 2
if cell.value = 3 then l = 3 etc...

if j = k then ai.value = ai.value + (Winner*2) and so on...I don't remember the error I got but I want to say it was similar...

Im guessing you just cant take a group of cells and test them this way...any help would be appreciated.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe:
Code:
Sub test()
    Dim bl As String, cl As String, dl As String, ai As Range, Winner As Integer

    bl = Range("B2") & "|" & Range("C2") & "|" & Range("D2")
    cl = Range("B3") & "|" & Range("C3") & "|" & Range("D3")
    dl = Range("B4") & "|" & Range("C4") & "|" & Range("D4")
    
    If bl = cl Or cl = dl Or dl = bl Then
        ai.Value = ai.Value + (Winner * 2)
    End If
End Sub
 
Upvote 0
Possibly !!
This code checks if any particular row matches with any other row in range ("B2 to D4")
Code:
[COLOR="Navy"]Sub[/COLOR] MG23Mar45
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("B2:B4")
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Txt = Join(Application.Index(Dn.Resize(, 3).Value, 0, 0), ",")
        [COLOR="Navy"]If[/COLOR] Not .Exists(Txt) [COLOR="Navy"]Then[/COLOR]
            .Add Txt, Dn
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] .Item(Txt) = Union(.Item(Txt), Dn)
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, Fd [COLOR="Navy"]As[/COLOR] Boolean

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]If[/COLOR] .Item(K).Count > 1 [COLOR="Navy"]Then[/COLOR]
        MsgBox "Matching numbers where found at = " & .Item(K).Address _
        & vbLf & "The winning numbers where = " & K
      Fd = True
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]If[/COLOR] Not Fd [COLOR="Navy"]Then[/COLOR] MsgBox "No Matching Numbers"
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
For some reason it won’t let me see the code when I touch the view code button it takes me back this page.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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