VBA comparing Cells

Faygin

New Member
Joined
May 12, 2020
Messages
23
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Need help with this.
Looked through some similar posts but couldnt figure out how to adapt them.

I got a value in cell B2 and a value in Cell B1 (a value between 0-10).

They are values that are not allowed to be the same.

If the value in B2 ends up to be the same as in cell B1 i need to call the sub (Sub CalculateFoe) that are randomizing a new number for B2.

Then test this again, if the randomized value in cell B2 are the same as in B1 then repeat, otherwise end sub.

I want to be able to write this in VBA.
 

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.
paste this code in your worksheet module like where the Red square is in the picture
Snag_1398c2a6.png

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim randomNum As Long
    If Range("B1").Value = Range("B2").Value Then
        randomNum = Int(10 * Rnd) + 1
        Range("B2").Value = randomNum
    End If
End Sub
 
Upvote 0
Thank you so much!

I looked at your example to get a understanding.

And this litte beauty did the trick:
--
If Range("B2").Value = Range("B1").Value Then
Call CalculateFoe
End If
--
 
Upvote 0
you dont need to call your sub procedure if you are just changing the number to a random number. Thats what my code does. If you call another sub procedure it is slower than what I typed.
 
Upvote 0
I understand, thank you i will investigate it further.

My fear was that your code could generate a new randomized number that still makes cell B2 a equal value that is in B1.

So i wanted to create a loop where it generates a new number until they are separate values.
 
Upvote 0
My fear was that your code could generate a new randomized number that still makes cell B2 a equal value that is in B1.
Your fear is real, that's exactly what could happen.
 
Upvote 0
Your fear is real, that's exactly what could happen.

Thank you, now that i understand how to compare the two values and call the randomizer routine if they are equal, the problem is solved.
Thank you all for your valuable help!
 
Upvote 0

Forum statistics

Threads
1,216,165
Messages
6,129,235
Members
449,496
Latest member
Patupaiarehe

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