Hi, I have this macro to create and I have written most of it but it is far from perfect...I don't know what's wrong with it though. I'm just starting with VBA
Write a program for a guessing game played by two players. Hence read two numbers as input from the user.
The program will now tell (Using MsgBox) to the second player how many digits are rightly placed and how many digits exist in the first number but are misplaced
E.g. "There is 1 number well placed and 1 number misplaced"
Keep going with the program telling the second player how many digits are placed/misplaced and ask the second player to give numbers until he finds the right one.
If the 3 digits are in the right place the program stops by displaying "Bravo you have found, the number is ..." else the program requests another number from the second player until it finds the right number.
I have this for now
x = InputBox("player 1, enter an integer number")
Do Until x = y
y = InputBox("player 2, enter an integer number")
For i = 1 To Len(x)
a = Mid(x, i, 1)
For j = 1 To Len(y)
b = Mid(y, j, 1)
If a = b And i = j Then
count1 = count1 + 1
ElseIf a = b And i <> j Then
count2 = count2 + 1
If count1 = 0 And count2 < 2 Then
MsgBox ("There is 0 digit well placed and 0 digit misplaced")
ElseIf count1 = 0 And count2 >= 2 Then
MsgBox ("There is 0 digit well placed and" & count2 & " digits misplaced.")
ElseIf x = y Then
MsgBox "There are " & count1 & " digits well placed and 0 digits misplaced. Bravo, you have found the right number!"
Apparently there's a problem with the line count1 = count1 + 1
Thanks for your help
Turn on thread page Beta
VBA Macro exercise help watch
- Thread Starter
- 02-02-2017 20:21