I see some discrepancies in your latest response and the responses and code you posted earlier. In the code, and in your earlier posts, the range to hold your randomly selected numbers was B25:B30, and the 0,1 indicators for whether a number appears among the randomly selected numbers are in B19:B24. Also, in the code, an input value of 1 in cell B4 results in all zeroes being returned, whereas an input of 7 does this per your latest post. For the sake of discussion, I'll assume the latest post is what you want.
Now some comments about your code. First, it would be a great help to others in the future if you could take a few minutes to learn how to use the code tags around your code. You can use the code button above the input window to generate the tags. I didn't spend too much time trying to understand your code for that reason. After further review, the code was generating further worksheet change events but these did not continue indefinitely. That wasn't your problem, but still as a matter of good practice you should disable application events if your worksheet change event code changes the sheet itself.
You still didn't really clarify what you meant by "it seemed to be looping and not stopping." However, after further study, I may have a guess. If you request 1 to 5 random numbers to be generated, your code will always produce 5 results. regardless of the input value in B4. Look at your code in the case 2 to 6 branch. It always loops from x = 1 to x=5 and always writes 5 results to the worksheet. You don't even use the value of n (in which you stored the input value in cell B4).
In that branch of the select, you generate a random integer r, store that number in an element of array num, and then store that element of the num array in the worksheet. Why do you need the array num, or even a single variable? You can eliminate the middleman and just store the result r directly in the worksheet.
In that same branch of the code, you have an array named "used" which you use to prevent reselection of a previously selected number. Now, if you think about it a bit, you should realize that these "used" flags are exactly what you are trying to compute in your initial post on this thread. I would suggest using the desired range in the worksheet itself to house the "used" flags, and you won't need to worry about COUNTIF at all.
Finally, I have no idea what you're trying to accomplish in your code with MY_FIRST, MY_SECOND, etc and it doesn't appear necessary to accomplish what you want in your last post. All of this leads to my suggested code below. Note the use of range object variables. This isn't strictly necessary, but it requires changing only two or three statements instead of many if you have to move your ranges around for any reason.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$B$4" Then Exit Sub
Dim InputValue As Integer, i As Integer
Dim rUsedFlags As Range, rRandomNumbers As Range
Application.EnableEvents = False
Range("B23:B29").ClearContents 'not too sure about this
Set rUsedFlags = Range("B17:B22")
Set rRandomNumbers = Range("B23:B28")
InputValue = Range("B4").Value
'initialize used flags to 0, unless input value = 6
rUsedFlags.Value = -(InputValue = 6)
Select Case InputValue
Case 1 To 5
Dim r As Integer
For i = 1 To InputValue
Do
r = Int(Rnd * 6) + 1 'random integer, 1 to 6
Loop Until rUsedFlags(r) = 0
rRandomNumbers(i).Value = r
rUsedFlags(r) = 1
Next
Case 6
For i = 1 To 6
rRandomNumbers(i).Value = i
Next
Case 7
rRandomNumbers.Value = 0
End Select
Application.EnableEvents = True
End Sub