通过循环随机填写行

时间:2019-09-27 13:43:07

标签: excel vba

我的值在“ A1:O1”范围内。 每个列在此范围内都有一个唯一值。 我需要帮助来开发一个循环,该循环将在同一列的顶部值(列值)中填充04次。伪代码下方

Sub FillDownRowsRandomly()
 Dim i As Integer, j As Integer

 'RamdomRow=Total of 04 unique ramdom numbers
 'choosen from 01 to 06 {1,2,3,4,5,6}
 'meaning that in a loop of 6 interations, when fill down
 '2 will be Null or empty
 '
 For i = 1 To 15 'Columns "A" To "O"
     For j = 2 To 7 '
     '
     Cells(RandomRow, i).Value = Cells(1, i).Value
     Next j
 Next i
 End Sub

在图像下方,将可能标识代码结果。 忽略单元格中写的“空”字。我写这只是为了阐明在随机循环中,代码“忽略了该单元格”。

ImageShowingHowTheResultMightBe

2 个答案:

答案 0 :(得分:1)

也许像这样:

Sub FillDownRowsRandomly()

Dim x As Long, y As Long, z As Long

With Sheet1 'Change accordingly
    For y = 1 To 15
        z = 0
        Do While z < 4
            x = Int((7 - 2 + 1) * Rnd + 2)
            If .Cells(x, y) <> .Cells(1, y) Then
                .Cells(x, y) = .Cells(1, y)
                z = z + 1
            End If
        Loop
    Next y
End With

End Sub

答案 1 :(得分:0)

环绕列并随机放置值,直到六行中有四个为止。

dll

enter image description here