Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

excel - Fill down rows randomly by Loop

I have the values on the range "A1:O1". Each Column has a unique value in this range. I need help to develop a loop that will fill down 04 times on each column the same Top Value (Column Value). Below a Pseudo Code

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

Below an Image where will be possible identify the result of the code. Disregard the "Null" word written in the cells. I wrote that just to clarify that during the random loop, the code "ignored that cell".

ImageShowingHowTheResultMightBe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe something like:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...