【问题标题】:Setting cell equal to random value if cell isn't blank in range如果单元格在范围内不是空白,则将单元格设置为随机值
【发布时间】:2019-10-20 02:43:31
【问题描述】:

在高层次上,我试图将一个单元格设置为一个范围内的随机单元格。我遇到的问题是,在这个范围内我想从中提取一个随机值,我所取的值是一个“if”表达式的结果,它将单元格设置为一个值或“”。因此,当我选择随机值时,我只想选择具有实际值的单元格,而不是“”。

有谁知道如何获得这种预期的行为?

下面的代码显示了我目前尝试过的内容,每个大块都有注释以帮助理解。我需要帮助的块替换每一列中的值,直到下一个单元格为空白,然后移动到下一列。

upperBound = 1798
lowerBound = 2

Randomize

'This loop section populates the data area with a static value in cell 9,3 then 9,4 etc..
For j = 3 To 15
   val = Cells(9, j).Value
   For i = 1 To val
      Cells(12 + i, j).Value = Cells(9, j)
   Next i
Next j

'This loop section uses the cells already populated down each column and replaces that value with the random value from the other range
Dim x As Integer
' Set numrows = number of rows of data.
For j = 3 To 15
  NumRows = Range(Cells(13, j), Cells(13, j).End(xlDown)).Rows.Count
  ' Select cell 13,j.
  Cells(13, j).Select
  ' Establish "For" loop to loop "numrows" number of times.
  For x = 1 To NumRows
       ActiveCell.Value = Worksheets("2017 Role IDs").Cells(Int((upperBound - lowerBound + 1) * Rnd + lowerBound), 2).Value
     ' Selects cell down 1 row from active cell.
     ActiveCell.Offset(1, 0).Select
  Next
Next j

这是第二个块运行之前的数据。我想将与第二行中的数字匹配的值替换为范围内的随机数:

这就是我想要的样子:

但目前它看起来像这样,因为随机选择器采用空白值:

【问题讨论】:

  • 查看样本数据和预期输出会很有用
  • @cybernetic.nomad 已更新,感谢您的反馈!

标签: excel vba


【解决方案1】:

这样的东西应该适合你:

Sub tgr()

    Dim wb As Workbook
    Dim wsNums As Worksheet
    Dim wsDest As Worksheet
    Dim aData As Variant
    Dim vData As Variant
    Dim aNums() As Double
    Dim aResults() As Variant
    Dim lNumCount As Long
    Dim lMaxRows As Long
    Dim lRowCount As Long
    Dim ixNum As Long
    Dim ixResult As Long
    Dim ixCol As Long

    Set wb = ActiveWorkbook
    Set wsNums = wb.Worksheets("2017 Role IDs")
    Set wsDest = wb.ActiveSheet

    With wsNums.Range("B2", wsNums.Cells(wsNums.Rows.Count, "B").End(xlUp))
        If .Row < 2 Then Exit Sub   'No data
        lNumCount = WorksheetFunction.Count(.Cells)
        If lNumCount = 0 Then Exit Sub  'No numbers
        ReDim aNums(1 To lNumCount)
        If .Cells.Count = 1 Then
            ReDim aData(1 To 1, 1 To 1)
            aData(1, 1) = .Value
        Else
            aData = .Value
        End If

        'Load populated numeric cells into the aNums array
        For Each vData In aData
            If Len(vData) > 0 And IsNumeric(vData) Then
                ixNum = ixNum + 1
                aNums(ixNum) = vData
            End If
        Next vData
    End With

    lMaxRows = Application.Max(wsDest.Range("C9:O9"))
    If lMaxRows = 0 Then Exit Sub   'Row count not populated in row 9 for each column
    ReDim aResults(1 To WorksheetFunction.Max(wsDest.Range("C9:O9")), 1 To 13)

    'Populate each column accordingly and pull a random number from aNums
    For ixCol = 1 To UBound(aResults, 2)
        If IsNumeric(wsDest.Cells(9, ixCol + 2).Value) Then
            For ixResult = 1 To CLng(wsDest.Cells(9, ixCol + 2).Value)
                Randomize
                aResults(ixResult, ixCol) = aNums(Int(Rnd() * lNumCount) + 1)
            Next ixResult
        End If
    Next ixCol

    wsDest.Range("C13").Resize(UBound(aResults, 1), UBound(aResults, 2)).Value = aResults

End Sub

【讨论】:

  • 非常感谢,这正是我需要的方式。我对上述内容有一个问题:如果我希望随机数选择部分仅在其组匹配时选择随机数(因此,如果 wsDest 标题中的第 11 组与 wsNums 中的列 A 匹配)我将如何添加它?
  • @EmnutOggionni 您需要为每组随机数创建不同的数组。因此,对于要从中提取的每一列,您将拥有 aNumsHeader1、aNumsHeader2 等,而不仅仅是 aNums。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 2016-01-31
相关资源
最近更新 更多