Hi Everyone,
I have created some code, and what it should do is select row 5, starting in column F tot the last filled cell of that row, so for example F5:ZZ5 and change all values to "No". Next it prompts the user to enter a number, for example "20", it now puts 20 random values in the range F5:ZZ5 to "Yes".
So far it works decent, but because it puts random cells to "Yes", it could happen that it puts the same cell to "Yes" multiple times.
To solve this I've edited the code, I've counted the number of cells which contain the value "Yes" and if it was less then the number the user entered, the code runs again. This works fine if the user fills in the number 20 (at least when the range contains about 250 cells). But when the user fills in the number 100, there is no way it works, because the code always replaces all values in range with "No" again.
So what I wanted to do when the count of cells with value "Yes" is less then the user entered, is select all cells in range with value "No" and fill the remaining number with the value "Yes", also at random, so that the number that the user entered is always equal to the number of cells set to "Yes".
This part of the code, unfortunately, fails. It fails because the range contains skips, so for example F5 and G5 are set to no, H5 and I5 are set to yes and J6 is set to no. F5, G5 and J6 are selected. But for some reason I don't understand, the code also changes values in the next row. It seems like the more skips I have in the selection, the more rows are used.
So for example when I run the code with the above sample, F5 is set to Yes (which is correct) and F6 is also set to Yes (which not supose to happen, as the code should only run in the selection, which only contain cells in row 5).
Here is the code that I've used:
I have created some code, and what it should do is select row 5, starting in column F tot the last filled cell of that row, so for example F5:ZZ5 and change all values to "No". Next it prompts the user to enter a number, for example "20", it now puts 20 random values in the range F5:ZZ5 to "Yes".
So far it works decent, but because it puts random cells to "Yes", it could happen that it puts the same cell to "Yes" multiple times.
To solve this I've edited the code, I've counted the number of cells which contain the value "Yes" and if it was less then the number the user entered, the code runs again. This works fine if the user fills in the number 20 (at least when the range contains about 250 cells). But when the user fills in the number 100, there is no way it works, because the code always replaces all values in range with "No" again.
So what I wanted to do when the count of cells with value "Yes" is less then the user entered, is select all cells in range with value "No" and fill the remaining number with the value "Yes", also at random, so that the number that the user entered is always equal to the number of cells set to "Yes".
This part of the code, unfortunately, fails. It fails because the range contains skips, so for example F5 and G5 are set to no, H5 and I5 are set to yes and J6 is set to no. F5, G5 and J6 are selected. But for some reason I don't understand, the code also changes values in the next row. It seems like the more skips I have in the selection, the more rows are used.
So for example when I run the code with the above sample, F5 is set to Yes (which is correct) and F6 is also set to Yes (which not supose to happen, as the code should only run in the selection, which only contain cells in row 5).
Here is the code that I've used:
Code:
Function RandCell(Rg As Range) As Range Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
Sub RandCellTest()
Dim Counter As Long
Dim TargetRg As Range, Cell As Range
Dim TargetRg2 As Range
Dim TestCaseCount As Variant
'Ask for the # of test cases
TestCaseCount = InputBox("Specify the number of random testcases")
'Set all testcases to "No"
Range("F5").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Value = "No"
'Set entered number of test cases to "Yes"
Range("F5").Select
Set TargetRg = Range(Selection, Selection.End(xlToRight))
For Counter = 1 To TestCaseCount
Set Cell = RandCell(TargetRg)
Cell.Value = "Yes"
Next
'Check if the desired number of test cases has been reached
If Range("CountTestcases2").Value < TestCaseCount Then
' If not, select all testcases put to "No"
Range("F5").Select
Dim firstAddress As String, c As Range, rALL As Range
With Range(Selection, Selection.End(xlToRight))
Set c = .Find("No", LookIn:=xlValues)
If Not c Is Nothing Then
Set rALL = c
firstAddress = c.Address
Do
Set rALL = Union(rALL, c)
Range(c.Address).Activate
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
.Activate
If Not rALL Is Nothing Then rALL.Select
End With
' Put missing number of testcases to "Yes"
TestCaseCount = TestCaseCount - Range("CountTestcases2").Value
Set TargetRg2 = Selection
For Counter = 1 To TestCaseCount
Set Cell = RandCell(TargetRg2)
Cell.Value = "Ja"
Next
End If
End Sub