[英]Populating a Column of Cells with Values from Adjacent Cells (Excel VBA)
[英]Excel Return 4 random unique values to adjacent cells from a list of values
这是使用Googledocs完成的另一种解决方案
它涉及到使用JOIN,SPLIT,RANDBETWEEN,ADDRESS,ROW,INDIRECT,IF,LEFT,RIGHT,SUBSTITUTE和REPT的过程。这是一个迭代过程,从逗号分隔列表中删除一个值(分隔符由B4驱动。请确保它是一个字符)您的数据不包含)。 基本上,每次您选择随机值时,都将其从选择中删除。 因此,第一次有10种可能性,第二次有9种可能性,第三次有8种,依此类推...
修订版V2:在B4中使用SEPARATOR char,甚至更多使用SUBSTITUTE。 (通过在列之间复制然后重新组合的IF公式,减少了额外的中间步骤)。 结果: 配方 :
对于H2至H13中的数据,在I2至I13中输入:
=RAND()
然后在J18至M18中输入:
=INDEX($H$2:$H$13,RANK(I2,$I$2:$I$13,1)+COUNTIF($I$2:I2,I2)-1)
=INDEX($H$2:$H$13,RANK(I3,$I$2:$I$13,1)+COUNTIF($I$2:I3,I3)-1)
=INDEX($H$2:$H$13,RANK(I4,$I$2:$I$13,1)+COUNTIF($I$2:I4,I4)-1)
=INDEX($H$2:$H$13,RANK(I5,$I$2:$I$13,1)+COUNTIF($I$2:I5,I5)-1)
编辑#1:
Dim ary()
Sub Shuffle(InOut() As Variant)
Dim HowMany As Long, i As Long, J As Long
Dim tempF As Double, temp As Variant
Hi = UBound(InOut)
Low = LBound(InOut)
ReDim Helper(Low To Hi) As Double
Randomize
For i = Low To Hi
Helper(i) = Rnd
Next i
J = (Hi - Low + 1) \ 2
Do While J > 0
For i = Low To Hi - J
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = temp
End If
Next i
For i = Hi - J To Low Step -1
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = temp
End If
Next i
J = J \ 2
Loop
End Sub
Public Function Xclude(rX As Range, rng As Range) As Variant
Application.Volatile
Dim v As Variant, N As Long, i As Long
v = rX.Text
N = rng.Count
i = 1
For Each r In rng
v2 = r.Text
If v <> v2 Then
ReDim Preserve ary(1 To i)
ary(i) = v2
i = i + 1
End If
Next r
Call Shuffle(ary)
Xclude = ary
End Function
照明单元K18到M18 ,然后在编辑栏中单击。 然后输入数组公式:
=xclude(J18,H2:H13)
必须使用Ctrl + Shift + Enter输入 数组公式 ,而不仅仅是Enter键。 如果正确完成此操作,则公式将在公式栏中显示为带有大括号的公式。
感谢您的先前答复,但我不得不在代码中争取所需的结果。 我已经使用块在App Inventor中编码了类似的结构(我希望所有语言都有块),所以我将代码翻译成VBA。 如果有人使用它,以下是解决方案:
Option Base 1
Function RSec(rng As Range, kactane As Integer, Optional exclude As String = "NoneX")
'rng is the source, kactane shows how many items to return, optional exclude will be excluded if supplied)
'lng holds the number of items in the supplied range
Dim lng As Integer
'listholder will hold everything in range
Dim listholder As New Collection
'chosen is the final list that will provide the randomly selected items
Dim chosen As New Collection
'Ranno is the random number for list index
Dim RanNo As Integer
'result is the array to return values to cells
Dim result() As String
'1- Add all items in range to listholder
For i = 1 To rng.Count
listholder.Add rng.Item(i).Value
Next i
'2- print listholder length for debug purposes
'Debug.Print "Listholder uzunluğu:"; listholder.Count
'set lng to listholdercount
lng = listholder.Count
'set a random number
Randomize
RanNo = Int((lng - 1 + 1) * Rnd + 1)
'main loop to choose kactane number of items
For k = 1 To kactane
'check if exclude parameter is present
'if exclude parameter is not present, then choose randomly without checking
If exclude = "NoneX" Then
'add the randomly selected to the collection chosen
chosen.Add listholder(RanNo)
'remove the randomly selected from the list
listholder.Remove (RanNo)
'update the lng count
lng = listholder.Count
Else
'if exclude parameter is present and randomly selected item is equal to exclude
If listholder(RanNo) = exclude Then
'decrement the k value to repeat this step and choose another item
k = k - 1
'if exclude parameter is present but not equal to the randomly chosen
Else
'seçileni chosen'a ekle
chosen.Add listholder(RanNo)
'orjinal listeden çıkar
listholder.Remove (RanNo)
'lng'yi güncelle
lng = listholder.Count
End If
End If
're set to a new random number
Randomize
RanNo = Int((lng - 1 + 1) * Rnd + 1)
Next k
'set the size of the array
ReDim result(chosen.Count)
'push everything in collection to array
For rd = 1 To chosen.Count
result(rd) = chosen(rd)
Next rd
'return result
RSec = result
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.