我有一个带有问题的文本文件,每行一个。我希望 powerpoint 从文件中随机选择一行并将该行放入标签中。我还想确保每行只使用一次。如果没有简单的方法来解决这个问题,也许可以删除从文本文件中选择的行。我在网上找到了一些代码,但它不会做我想要的(不使用同一行两次)。
问问题
226 次
1 回答
0
尝试这个
Sub do_file()
Dim myArray
Open [YOUR TEXT FILE HERE] For Input As #1
fileinfo = Input(LOF(1), #1)
Close #1
myArray = Split(fileinfo, vbCrLf)
myArray = ShuffleArray(myArray)
For i = 0 To UBound(myArray)
[YOUR LABEL HERE] = myArray(i)
Next i
End Sub
Function ShuffleArray(OrigArray As Variant) As Variant
Dim RandNum As Long
Dim Holder As Variant
Dim ReturnArray() As Variant
ReDim ReturnArray(LBound(OrigArray) To UBound(OrigArray))
For i = LBound(OrigArray) To UBound(OrigArray)
ReturnArray(i) = OrigArray(i)
Next i
For i = LBound(OrigArray) To UBound(OrigArray)
RandNum = Int((UBound(OrigArray) - LBound(OrigArray)) * Rnd + LBound(OrigArray))
If i <> RandNum Then
Holder = ReturnArray(i)
ReturnArray(i) = ReturnArray(RandNum)
ReturnArray(RandNum) = Holder
End If
Next i
ShuffleArray = ReturnArray
End Function
这将读取文件,将每一行放入一个数组中,然后将数组随机播放。然后它将循环通过洗牌数组,您需要在其中包含标签以输出问题。
于 2013-11-05T20:13:01.973 回答