You are Here: Home

# VBA Macro exercise help watch

Announcements
1. 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

Exercise is
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
End If
Next j
Next i

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!"

End If

Loop

Apparently there's a problem with the line count1 = count1 + 1

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: February 2, 2017
Today on TSR

### Exam Jam 2018

Join thousands of students this half term

### Solo - A Star Wars Story

Poll
Useful resources

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE