【问题标题】:Fill columns randomly by Loop通过循环随机填充列
【发布时间】:2019-10-04 17:34:18
【问题描述】:

最近,我得到了一个代码,该代码根据 Row#1 值随机填充单元格。 Answered_Post。 (感谢@JvdV 和@Scott Craner 之前对我的帮助。)

我现在需要做的几乎相同,但代码将按照随机值 (x) 填充跨越列的单元格,总共 10 行。可重复的值保留在第 1 行。

在该帖子提供的代码下方以填写行。我现在需要根据图片填充列。

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

Table_With_Sample_Values

【问题讨论】:

  • 对列的变量做同样的事情。
  • 你能提供一些样品吗?
  • 我明白你的意思。我尝试了不同的代码,但我卡住了。

标签: excel vba loops


【解决方案1】:
Sub FillColumns01()
    For y = 1 To 15
    z = 0
    j = 1
    Do While z < 4
            x = Int((7 - 2 + 1) * Rnd + 2)
            If Cells(x, y) <> Cells(1, y) Then
                Cells(y + 1, j) = Cells(1, y)
                z = z + 1
                j = j + 1
            End If
    Loop
Next y
End Sub

Sub FillColumns02()
'Using a 3rd Loop
Dim x As Integer, y As Integer, z As Integer, j As Integer

For y = 1 To 10
    z = 0
    Do While z < 4
        For j = 1 To 15
            x = Int((7 - 2 + 1) * Rnd + 2)
            If Cells(x, y) <> Cells(1, y) Then
                Cells(j, x) = Cells(1, y)
                z = z + 1
            End If
        Next j
    Loop
Next y

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2020-06-07
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多