Sub CopyRandomRows()
Sheets("Random Sample").Select
Cells.Select
Range("C14").Activate
Selection.Delete Shift:=xlUp
Windows("Critical Infotype Raw Data.xlsx").Activate
Rows("1:1").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Windows("Critical Infotype Sampling Tool.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&
' this defines the source to take the data
Set source = Workbooks("Critical Infotype Raw Data.xlsx").Worksheets("Sheet1").Range("A2:L5215")
' this defines the target to paste the data
Set target = Workbooks("Critical Infotype Sampling Tool.xlsm").Worksheets("Random Sample").Range("A2")
' this defines the number of rows to generate based on the input in textbox
randCount = Worksheets("Main").TextBox1.value
' this load the data in an array
data = source.value
'this shuffle the rows
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
' this writes the data to the target
target.Resize(randCount, UBound(data, 2)) = data
MsgBox "Random Sample Generated!"
End Sub
Oben ist mein ganzer Code zu bekommen/generieren Stichprobe Daten aus einer anderen Arbeitsmappe basierend auf meinem Input in meiner Textbox.VBA/Makro Dynamikbereich/ignore Rohlinge
Mein Problem ist dieser Code:
'dies die Quelle definiert die Daten
Set source = Workbooks("Critical Infotype Raw Data.xlsx").Worksheets("Sheet1").Range("A2:L5215")
Ich möchte den Bereich nehmen nicht spezifisch sein, so dass ich alle Daten und Stichprobe erhalten können, ohne Problem haben. Außerdem, wenn ich nur 10 Daten habe, gibt es mir immer Leerzeichen, weil es sogar die Zeilen mit Leerzeichen innerhalb des Bereichs bekommt, also möchte ich, dass der Bereich nicht spezifisch ist. Wie kann ich es tun? Vielen Dank im Voraus.