从Excel中的下拉列表中选择随机项。

huangapple go评论105阅读模式
英文:

select random item from a drop down list in excel

问题

我有一个包含只能从下拉列表中选择项目的Excel表格,我想要随机填充它。

从Excel中的下拉列表中选择随机项。

网上的许多解决方案选择了一个列表中的随机值(而不是下拉列表),这对我来说不起作用,因为我的下拉列表依赖于其他单元格,所以它们会根据它们而变化。我想要的是从每个新的下拉列表中随机选择项目。

英文:

I have an Excel sheet with cells that can be filled only with items from the dropdown list and I want to fill it randomly

从Excel中的下拉列表中选择随机项。]

Many of the solutions on the web select a random value from a list (not a dropdown list) this does not work for me because my drop-down lists are dependent on other cells so they change based on them what i want is to randomly select items from each new dropdown list

答案1

得分: 2

在我的示例中,单元格A1具有验证列表。当用户选择该单元格时,它会从其验证列表中获取一个随机值:

  1. Option Explicit
  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3. Dim v As Variant
  4. If Target.CountLarge = 1 Then
  5. 'If Target.Address = Range("A1").Address Then
  6. If Target.Column = 1 Then ' 1中的所有单元格(A
  7. v = getRandomValue(Target, Me)
  8. If v <> vbNullString And v <> Target.Value2 Then
  9. Application.EnableEvents = False
  10. Target.Value2 = v
  11. Application.EnableEvents = True
  12. End If
  13. End If
  14. End If
  15. End Sub
  16. ' 从随机单元格获取值,使用单元格的验证列表
  17. Public Function getRandomValue(r As Range, ws As Worksheet) As Variant
  18. Dim s As String, ir As Range
  19. getRandomValue = vbNullString
  20. If hasValidationList(r) Then
  21. s = Trim(r.Validation.Formula1)
  22. Set ir = ws.Range(Trim(Mid(s, 2)))
  23. getRandomValue = ir.Cells(WorksheetFunction.RandBetween(1, ir.CountLarge)).Value2
  24. End If
  25. End Function
  26. ' 辅助函数 - 检查单元格是否具有验证列表
  27. Public Function hasValidationList(r As Range) As Boolean
  28. Dim t As XlDVType
  29. On Error Resume Next
  30. t = r.Validation.Type
  31. On Error GoTo 0
  32. hasValidationList = (t = xlValidateList)
  33. End Function
  34. ' 一个示例,用随机值填充A1:A1000中具有验证列表的所有单元格
  35. ' 将此复制到工作表的模块中(不要放在普通模块中)
  36. Private Sub fillAllCellsRandomly()
  37. Dim lastRow As Long, rng As Range, rr As Range, v As Variant
  38. lastRow = 1000
  39. Set rng = Me.Range("A1:A" & lastRow)
  40. For Each rr In rng
  41. v = getRandomValue(rr, Me)
  42. If v <> vbNullString Then rr.Value2 = v
  43. Next
  44. End Sub
英文:

In my example cell A1 has a validation list. When the user selects the cell, it gets a random value from its validation list:

  1. Option Explicit
  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3. Dim v As Variant
  4. If Target.CountLarge = 1 Then
  5. &#39;If Target.Address = Range(&quot;A1&quot;).Address Then
  6. If Target.Column = 1 Then &#39; all cells in column 1 (A)
  7. v = getRandomValue(Target, Me)
  8. If v &lt;&gt; vbNullString And v &lt;&gt; Target.Value2 Then
  9. Application.EnableEvents = False
  10. Target.Value2 = v
  11. Application.EnableEvents = True
  12. End If
  13. End If
  14. End If
  15. End Sub
  16. &#39; GET A VALUE FROM A RANDOM CELL, OF CELL&#39;s VALIDATION LIST
  17. Public Function getRandomValue(r As Range, ws As Worksheet) As Variant
  18. Dim s As String, ir As Range
  19. getRandomValue = vbNullString
  20. If hasValidationList(r) Then
  21. s = Trim(r.Validation.Formula1)
  22. Set ir = ws.Range(Trim(Mid(s, 2)))
  23. getRandomValue = ir.Cells(WorksheetFunction.RandBetween(1, ir.CountLarge)).Value2
  24. End If
  25. End Function
  26. &#39; HELPER FUNCTION - CHECK IF A CELL HAS VALIDATION LIST
  27. Public Function hasValidationList(r As Range) As Boolean
  28. Dim t As XlDVType
  29. On Error Resume Next
  30. t = r.Validation.Type
  31. On Error GoTo 0
  32. hasValidationList = (t = xlValidateList)
  33. End Function
  34. &#39; AN EXAMPLE TO FILL ANY CELLS IN A1:A1000 WHITCH HAVE A VALIDATION LIST
  35. &#39; WITH A RANDOM VALUE FROM THEIR LIST
  36. &#39;COPY THIS IN THE SHEET&#39;S MODULE (NO IN A COMMON MODULE)
  37. Private Sub fillAllCellsRandomly()
  38. Dim lastRow As Long, rng As Range, rr As Range, v As Variant
  39. lastRow = 1000
  40. Set rng = Me.Range(&quot;A1:A&quot; &amp; lastRow)
  41. For Each rr In rng
  42. v = getRandomValue(rr, Me)
  43. If v &lt;&gt; vbNullString Then rr.Value2 = v
  44. Next
  45. End Sub

huangapple
  • 本文由 发表于 2023年7月23日 20:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定