0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel VBA根据条件随机抽取符合条件的数据

Posted at

有存放抽取用数据的数据表,还有用于制作数据的当前表
此程序先用Select找到判断条件,之后进入符合判断条件的Case,
然后在该Case内设置抽取数据用的数据表的值的范围的行号,之后生成范围内随机数(随即行号),
再用随机数(行号)从数据表中抽出值

Sub 根据条件随机设置数据()
    Dim iMax As Double
    Dim iMin As Double
    col = "L"
    For i = 1 To 1000
        Select Case Cells(i, "K")
            Case "条件1"
                iMax = 4
                iMin = 2
                ret = Format(Fix((iMax - iMin + 1) * Rnd + iMin), "0")
                Cells(i, col) = ThisWorkbook.Worksheets("数据表名").Cells(ret, "A")
            Case "条件2"
                iMax = 5
                iMin = 2
                ret = Format(Fix((iMax - iMin + 1) * Rnd + iMin), "0")
                Cells(i, col) = ThisWorkbook.Worksheets("数据表名").Cells(ret, "B")
            Case "条件3"
                iMax = 41
                iMin = 2
                ret = Format(Fix((iMax - iMin + 1) * Rnd + iMin), "0")
                Cells(i, col) = ThisWorkbook.Worksheets("数据表名").Cells(ret, "C")
        End Select
    Next i
End Sub

这种做法的一个问题是,有多少个条件,就需要在数据表中放多少列

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?