【问题标题】:Pick random number from a range of cells, some of which are empty cells从一系列单元格中选择随机数,其中一些是空单元格
【发布时间】:2022-10-08 05:39:24
【问题描述】:

我正在尝试从一系列值中选择一个随机值并将该值输出到单元格 E6 中。

有些单元格是空白的,所以我需要从一个包含值的单元格中选择。

可供选择的值的范围是 H127:1127。

Sub Generate()
    Dim i As Double
    Dim ws As Worksheet
    
    Set ws = Sheets("Upstream-Overall")
    
    For Each Cell In ws.Range("H127:H1127")
        If ActiveCell.Value <> "" Then
            Range("E6") = Random_Number = Application.WorksheetFunction.RandBetween(0.1, 5)
        End If
    Next Cell
    
End Sub

【问题讨论】:

    标签: excel vba random


    【解决方案1】:

    从随机单元格中返回数字

    Sub Generate()
        
        Const wsName As String = "Upstream-Overall"
        Const sRangeAddress As String = "H127:H1127"
        Const dCellAddress As String = "E6"
        
        ' Reference the worksheet in the workbook containing this code.
        Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(wsName)
        
        ' Write the values from the first column of the range to an array.
        Dim Data As Variant: Data = ws.Range(sRangeAddress).Columns(1).Value
        
        Dim sr As Long, dr As Long
        
        ' Shift the numeric values (up) to the beginning of the array.
        For sr = 1 To UBound(Data, 1)
            If VarType(Data(sr, 1)) = 5 Then ' is a number
                dr = dr + 1
                Data(dr, 1) = Data(sr, 1)
            'Else ' is not a number; do nothing
            End If
        Next sr
        
        ' Check if at least one number was found.
        If dr = 0 Then
            MsgBox "No numbers in the first column of the range.", vbCritical
            Exit Sub
        End If
        
        ' Write the number from a random element
        ' of the numeric part of the array to the cell.
        ws.Range(dCellAddress).Value = Data(Int(dr * Rnd + 1), 1)
            
        ' Inform of success.
        MsgBox "New random number generated.", vbInformation
            
    End Sub
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      Sub Generate()
          Dim ws As Worksheet, rng As Range, i As Long, v
          
          Set ws = Sheets("Upstream-Overall")
          Set rng = ws.Range("H127:H1127")
          Do
              i = Application.RandBetween(1, rng.Cells.Count)
              v = rng.Cells(i).Value
          Loop While Len(v) = 0  'loop until selected cell has a value
          
          ws.Range("E6").Value = v
           
      End Sub
      

      (假设范围永远不会完全为空)

      【讨论】:

      • 如果 - 我想从不同的范围生成一个数字(这个范围仍然在 Upstream-Overall 表中),但是我希望生成的随机数(我现在知道该怎么做)显示在例如表 2 中的单元格 E14? ws2.Range("E14").Value = v 这似乎不起作用(我将 ws2 设置为我希望在其中显示随机编号的工作表)
      • 如果生成随机值的按钮在 Upstream-Overall 工作表中,它可以工作,但是如果按钮在随机数输出所在的实际工作表中,则它不起作用
      • 您可以更新您的问题并添加您尝试过的代码。如果所有范围都完全符合工作表对象,那应该没问题。还有“不起作用”到底是什么样的?你有错误吗?
      猜你喜欢
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2022-11-17
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多