我有一个带问题的文本文件,每行一个。我希望powerpoint从文件中随机选择一行并将该行放入标签中。我还想确保每行只使用一次。如果没有简单的方法可以解决这个问题,可能是一种删除从文本文件中选择的行的方法。我在网上找到了一些代码,但它不会做我想做的事情(不使用同一行两次)。
答案 0 :(得分: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
这将读取将每行放入数组的文件,然后将随机播放该数组。然后它将循环遍历洗牌数组,您需要在其中包含用于输出问题的标签。