关闭。这个问题需要details or clarity .它目前不接受答案。
想改进这个问题?通过 editing this post 添加详细信息并澄清问题.
6年前关闭。
Improve this question
任何人都可以显示一个从 Excel 中的句子中获取随机单词的函数。我正在做一个填空练习。 A1 列包含一长串句子(每个句子在一行上)。我需要从 A1 的每个句子中获取 B1 列包含列表或随机单词。我一直在寻找很长一段时间,但没有找到任何可能的东西。另外,如果可以在 Excel 中完成,速度会比其他的快得多。我认为。
最佳答案
这是我的自定义函数,它添加了一些额外的功能。Public Function GetRandomWords(TheCell As Range, Optional AllowDuplicates As Boolean, Optional NumberOfWords As Integer, Optional Delimiter As String) As String
基本上,它将从单元格中获取随机数量的单词(介于 1 和单元格中的最大单词数之间)。
默认情况下,它不允许重复,会获取随机数量的单词,并使用 excel 默认分隔符。
您可以更改要获取的随机单词数、分隔符以及是否允许重复。
您可以使用 CTRL
重新计算公式+ ALT
+ SHIFT
+ F9
您可以使用 CTRL
在输入公式时显示参数+ SHIFT
+ A
Public Function GetRandomWords(TheCell As Range, Optional AllowDuplicates As Boolean, Optional NumberOfWords As Integer, Optional Delimiter As String) As String
If TheCell.CountLarge > 1 Then Set TheCell = TheCell.Cells(1, 1)
Dim Words() As String
Dim NixDupe As New Collection
Dim Helper As Integer
Dim Final As String
Dim x As Integer
If Delimiter = "" Then
Words = Split(TheCell.Value)
Else
Words = Split(TheCell.Value, Delimiter:=Delimiter)
End If
For x = 0 To UBound(Words)
NixDupe.Add Words(x)
Next x
If NumberOfWords = 0 Then
NumberOfWords = Int((NixDupe.Count) * Rnd + 1)
End If
If (NumberOfWords > NixDupe.Count And Not AllowDuplicates) Or NumberOfWords < 0 Then NumberOfWords = NixDupe.Count
Final = ""
For x = 1 To NumberOfWords
Helper = Int((NixDupe.Count) * Rnd + 1)
Final = Final & NixDupe(Helper) & " "
If Not AllowDuplicates Then
NixDupe.Remove (Helper)
If NixDupe.Count = 0 Then Exit For
End If
Next x
GetRandomWords = Trim(Final)
End Function
使用
=GetRandomWords(A1)
使用
=GetRandomWords(A1,FALSE,3)
还有一个使用绝对单元格引用的不同句子。
=GetRandomWords($A$1,FALSE,1)
关于excel - 我可以使用 Excel 函数从句子中生成随机单词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603404/