[英]copy and paste with certain column condition vba excel
I have a problem with Excel VBA coding. 我对Excel VBA编码有问题。
I want to make one coding may copy data from one sheet to a sheet with certain conditions. 我想使一种编码可以在一定条件下将数据从一张纸复制到一张纸上。 my data in the form binary.
我的数据采用二进制形式。
data in sheet1 has nearly a thousand row. sheet1中的数据有近一千行。 I just want to take 15 random row of data from sheet1 to sheet 2. The criteria which must be fulfilled is that one specific column the sum of the column is 12. if not met, other data will be taken.
我只想从Sheet1到Sheet 2随机抽取15个数据行。必须满足的条件是,一个特定的列的总和为12。如果不满足,则将采用其他数据。
Example of data 数据示例
Here is my coding, but it doesn't work. 这是我的编码,但是不起作用。
dim clm , ClmTtl as integer
for row = 1 to 1000
ClmTtl = ClmTtl + Sheets(1).Cells(row,8).Value
next
if not ClmTtl = 12 then call CommandButton1_click
er` `
To take a random (pseudorandom, to be accurate) number, you can use Rnd
function. 要获取随机数(准确地说是伪随机数),可以使用
Rnd
函数。 It gives you a value in range form 0 to 1. To get some particular number from range, you can use this solution . 它为您提供0到1范围内的值。要从范围中获取某个特定数字,可以使用此解决方案 。 So, to get a random number from your range of row 2 to last , you can do for example:
因此,要从第2行到last的范围中获取一个随机数,可以执行例如:
Dim LastRow As Long, rRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row ' Last row based on column A
rRow = Int((LastRow - 2 + 1) * Rnd + 2)
Now you need to take this 15 times, and be sure that you wont get the same row multiple times. 现在,您需要进行15次操作,并确保不会多次获得同一行。 We can use array, and store row numbers inside.
我们可以使用数组,并在其中存储行号。 Unfortunately, VBA has no function to check if some particular value is inside.
不幸的是,VBA没有功能来检查是否存在某些特定值。 We to do it by looping through the values.
我们通过遍历值来做到这一点。
Dim LastRow As Long, rRow As Long
Dim rowArr(14) As Long
Dim found As Boolean
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 0 To 14
rRow = Int((LastRow - 2 + 1) * Rnd + 2)
found = False
If i > 0 Then
For j = i - 1 To 0 Step -1
If rowArr(j) = rRow Then found = True
Next j
End If
If found Then
i = i - 1
Else
rowArr(i) = rRow
End If
Next i
No we have to check, if sum of values in random rows are equal to 12, and if not, loop the whole process. 否,我们不必检查随机行中的值的总和是否等于12,否则,请循环执行整个过程。 The whole thing will look like:
整个过程看起来像:
Dim LastRow As Long, rRow As Long
Dim rowArr(14) As Long
Dim found As Boolean, mainCriterium As Boolean
Dim sumOfValues As Double
mainCriterium = False
Do While mainCriterium = False
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 0 To 14
rRow = Int((LastRow - 2 + 1) * Rnd + 2)
found = False
If i > 0 Then
For j = i - 1 To 0 Step -1
If rowArr(j) = rRow Then found = True
Next j
End If
If found Then
i = i - 1
Else
rowArr(i) = rRow
End If
Next i
For i = 0 To 14
sumOfValues = sumOfValues + Range("G" & rowArr(i)).Value
Next i
If sumOfValues = 12 Then mainCriterium = True
Loop
When loop will end, you gonna have array rowArr
containing 15 rows, which sum of values in column G is equal to 12. 当循环结束时,您将拥有包含15行的数组
rowArr
,G列中的值之和等于12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.