Sub CopyRandomRows()
Windows("sample rnd.xlsm").Activate
Rows("1:1").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Windows("rnd sample draft.xlsm").Activate
Sheets("Random Sample").Select
Rows("1:1").Select
ActiveSheet.Paste
Dim source As Range, target As Range, randCount&, data(), value, r&, rr&, c&
Set source = Workbooks("sample rnd.xlsm").Worksheets("Sheet1").Range("A2:L5215")
Set target = Workbooks("rnd sample draft.xlsm").Worksheets("Random Sample").Range("A2")
randCount = 5
data = source.value
For r = 1 To randCount
rr = 1 + Math.Round(VBA.rnd * (UBound(data) - 1))
For c = 1 To UBound(data, 2)
value = data(r, c)
data(r, c) = data(rr, c)
data(rr, c) = value
Next
Next
target.Resize(randCount, UBound(data, 2)) = data
End Sub
这是我的代码。我的问题是,如果我更改代码,我只能更改我想要的数据数量randCount = 5
。我希望能够使用我的 TextBox 并使用它来定义要获取多少数据。我试过了randCound = TextBox1.value
,randCount = TextBox1.Text
但似乎没有用。我错过了什么?我如何让它工作。提前致谢