简体   繁体   English

Select 并使用 VBA 突出显示一个随机单元格

[英]Select and highlight one random cell using VBA

I currently have an excel spreadsheet that when a user clicks Go, it assigns a new random number to the cells in the worksheet.我目前有一个 excel 电子表格,当用户单击 Go 时,它会为工作表中的单元格分配一个新的随机数。 The range value is between 1 and 500 in a 20 by 25 matrix.范围值在 20 x 25 矩阵中的 1 到 500 之间。 I want to randomly select and change the background color to red for only one cell every time a user clicks the 'Go' button.我想随机 select 并且每次用户单击“开始”按钮时仅将一个单元格的背景颜色更改为红色。 Code below is currently assigning random numbers to the cells and selecting and highlighting a random cell.下面的代码目前正在为单元格分配随机数,并选择并突出显示随机单元格。 However, when Go is clicked again the previously selected cell is still highlighted along with the newly selected cell.但是,当再次单击 Go 时,先前选择的单元格仍然与新选择的单元格一起突出显示。 How can I code it to only highlight the newly selected cell when clicking Go?单击 Go 时,如何对其进行编码以仅突出显示新选择的单元格?

Public Sub GenerateRandom()
    Set MyRange = Range("C4:AA23")
        For i = 1 To 500
            MyRange.Cells(i) = i
        Next
        For Each Cell In MyRange
            swapcell = 1 + Int(Rnd * 500)
            savedValue = Cell.Value
            Cell.Value = MyRange.Cells(swapcell).Value
            MyRange.Cells(swapcell) = savedValue
        Next

       With MyRange.Cells(1 + Int(Rnd * 500))
                MyRange.Cells(RndBetween(1, 500)).Interior.Color = vbRed        
      End With        
    End Sub

    Public Function RndBetween(ByVal Low, ByVal High) As Integer
       Randomize
       RndBetween = Int((High - Low + 1) * Rnd + Low)
    End Function

As mentioned above, clearing the range colour before you highlight a cell is the quickest way.如上所述,在突出显示单元格之前清除范围颜色是最快的方法。 But if the background colours of your cells are set to something else then the following should work:但是,如果您的单元格的背景颜色设置为其他颜色,那么以下应该可以工作:

Alternate Solution: Store the location and colour of the cell to highlight cell, then restore it's original colour on each run.替代解决方案:存储单元格的位置和颜色以突出显示单元格,然后在每次运行时恢复其原始颜色。 You would declare the location outside the sub so that it doesn't disappear once the sub ends.您将声明子外部的位置,以便在子结束后它不会消失。 This would help if your background colours are something else.如果您的背景颜色是其他颜色,这将有所帮助。 Problem with this is it only works during an Excel session, if you close and save the location would have been lost, unless you saved it to a hidden sheet = unnecessary complexity for this task.问题在于它仅在 Excel session 期间有效,如果您关闭并保存该位置将会丢失,除非您将其保存到隐藏工作表中 = 此任务不必要的复杂性。

    Dim OriginalCell As Range
    Dim OriginalCol

    Public Sub GenerateRandom()

    Dim myRange As Range
    Dim NewCell As Range

    Set myRange = Range("C4:AA23")

    For i = 1 To 500
        myRange.Cells(i) = i
    Next

    For Each Cell In myRange
        swapcell = 1 + Int(Rnd * 500)
        savedValue = Cell.Value
        Cell.Value = myRange.Cells(swapcell).Value
        myRange.Cells(swapcell) = savedValue
    Next

    ''''new code
    Set NewCell = myRange.Cells(RndBetween(1, MyRange.Cells.Count))

    If OriginalCell Is Nothing Then
        Set OriginalCell = NewCell
        OriginalCol = OriginalCell.Interior.Color
    Else
        OriginalCell.Interior.Color = OriginalCol
        Set OriginalCell = NewCell
        OriginalCol = OriginalCell.Interior.Color
    End If

    NewCell.Interior.Color = vbRed
    '''''

    End Sub

On a side note, sending ranges to array and working with the array is much faster, but that's another topic.附带说明一下,将范围发送到数组并使用数组要快得多,但这是另一个主题。 Hope this helps!希望这可以帮助!

Clear the color of the range before setting the color of the random cell:在设置随机单元格的颜色之前清除范围的颜色:

Public Sub GenerateRandom()
    Set Myrange = Range("C4:AA23")
        For i = 1 To 500
            Myrange.Cells(i) = i
        Next
        For Each Cell In Myrange
            swapcell = 1 + Int(Rnd * 500)
            savedValue = Cell.Value
            Cell.Value = Myrange.Cells(swapcell).Value
            Myrange.Cells(swapcell) = savedValue
        Next

       Myrange.Interior.Color = xlNone
       With Myrange.Cells(1 + Int(Rnd * 500))
                Myrange.Cells(RndBetween(1, 500)).Interior.Color = vbRed
      End With
    End Sub

    Public Function RndBetween(ByVal Low, ByVal High) As Integer
       Randomize
       RndBetween = Int((High - Low + 1) * Rnd + Low)
    End Function

The Color question has been answered.颜色问题已得到解答。 But there are other issues in your code, notably that your shuffle is biased , as explained here但是您的代码中还有其他问题,特别是您的 shuffle 是有偏见的,如此所述

Here's a version that fixes the Modulo Bias mentioned in the link, together with a number of other issues这是一个修复链接中提到的模偏差的版本,以及一些其他问题

Public Sub GenerateRandom()
    'declare variables
    Dim MyRange As Range, Cell As Range
    Dim i As Long
    Dim swapcell As Long, savedValue As Long
    Dim idx As Long

    Randomize 'only need this once
    Set MyRange = ActiveSheet.Range("C4:AA23") 'or specify a specific sheet
    For i = 1 To MyRange.Cells.Count ' link size to specified range
        MyRange.Cells(i) = i
    Next
    For idx = MyRange.Cells.Count To 1 Step -1
        swapcell = RndBetween(1, idx) 'remove modulo bias
        savedValue = MyRange.Cells(idx).Value
        MyRange.Cells(idx).Value = MyRange.Cells(swapcell).Value
        MyRange.Cells(swapcell) = savedValue
    Next

    MyRange.Interior.Color = xlNone 'remove colour
    'removed unused With block
    MyRange.Cells(RndBetween(1, MyRange.Cells.Count)).Interior.Color = vbRed
End Sub

'declare types
Public Function RndBetween(ByVal Low As Long, ByVal High As Long) As Long
    RndBetween = Int((High - Low + 1) * Rnd + Low)
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM