Hi Ford
The main problem is that you'd Dimmed the variables as Local variables, i.e. the values only persist until the procedure's End Sub. You could either
1. Declare the variables as Public variables above the Procedure. In which case they will persist when the procedure has finished and will still contain a value the next time the procedure is run.
OR
2. Use IF...Then in a block not as a single line. e.g.
If housenumber = 1 Then
House1 = House1 + Points
End If
This means the If tests for the two house numbers that are not in play don't ever get actioned. With the single line IF...Then construct then the succeeding line for all three get actioned, i.e.
Another way is with the Select Case statement which I generally prefer, particularly if there are a lot of IF statements. i.e.
Sub Add_To_Total()
Dim Points As Long, housenumber As Long
Points = InputBox("Enter Points")
housenumber = InputBox("Enter House Number (1, 2 or 3)")
Select Case housenumber
Case Is = 1
Range("A2") = Range("A2") + Points
Case Is = 2
Range("B2") = Range("B2") + Points
Case Is = 3
Range("C2") = Range("C2") + Points
End Select
End Sub
or a slight mod to the Select Case idea
Sub AddTotal()
Dim Points As Long, housenumber As Long, r As Range
Points = InputBox("Enter Points")
housenumber = InputBox("Enter House Number (1, 2 or 3)")
Select Case housenumber
Case Is = 1
Set r = Range("A2")
Case Is = 2
Set r = Range("B2")
Case Is = 3
Set r = Range("C2")
End Select
r = r + Points
End Sub
Bookmarks