Excel VBAコーディングに問題があります。do while loop in excel vbaエラー
1つのコードを作成して、あるシートから特定の条件のシートにデータをコピーすることができます。フォームバイナリで私のデータ。
sheet1のデータは、ほぼ1000行あります。私はちょうどシート1からシート2に15ランダムな行のデータを取るしたいと思います。満たさなければならない基準は、各列が列の合計が3であるということです。それが満たされなければ、他のデータは取り込めません。 ClmTtlが3にならなくなるまでループしたいのですが、どうすれば修正できますか? 私を助けてください。または私は他の方法で行うことができますか?
Randomize 'Initialize Random number seed
Dim MyRows() As Integer ' Declare dynamic array.
Dim numRows, percRows, finalClm, nxtRow, nxtRnd, chkrnd, copyRow As Integer
'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
'Application.EnableEvents = False
'Determine Number of Rows in Sheet1 Column A
numRows = Sheets(1).Cells(Rows.count, "A").End(xlUp).Row
'Get 20% of that number
percRows = 15
Dim clm, ClmTtl As Integer
'Allocate elements in Array
ReDim MyRows(percRows)
'Create Random numbers and fill array
Do While ClmTtl <> 3
For nxtRow = 1 To percRows
getNew:
'Generate Random number
nxtRnd = Int((numRows) * Rnd + 1)
'Loop through array, checking for Duplicates
For chkrnd = 1 To nxtRow
'Get new number if Duplicate is found
If MyRows(chkrnd) = nxtRnd Then GoTo getNew
Next
'Add element if Random number is unique
MyRows(nxtRow) = nxtRnd
Next
For clm = 1 To 5
ClmTtl = 0
For copyRow = 1 To percRows
ClmTtl = ClmTtl + Sheets(1).Cells(MyRows(copyRow), clm).Value
Next
Next
Loop
For copyRow = 1 To percRows
Sheets(1).Rows(MyRows(copyRow)).Copy _
Destination:=Sheets(3).Cells(copyRow, 1)
Next
'Application.Calculation = xlCalculationAutomatic
'Application.ScreenUpdating = True
'Application.EnableEvents = True
End Sub
エラーの説明が何と言っていますか? –
実際にはエラーはありません。出力だけで@DenTempleにしたいものが得られない –
'numRows = Sheets(1).Cells(Rows.count、" A ")行の' Rows'を修飾する必要があります。(xlUp).Row'の代わりにグローバルを使用します。また、 'Option Base 1'を使用していない場合は、' MyRows'に余分な要素があります。残りのコードに基づいて 'ReDim MyRows(1 To percRows) 'として割り当てられるべきでしょう。 – Comintern