简体   繁体   English

Excel VBA修改范围和选择随机单元格

[英]Excel VBA Modifying Ranges and Selecting Random Cells

I have a cell range that I am selecting one single cell from, removing the cell from the range, and then selecting another random cell. 我有一个单元格范围,我从中选择一个单元格,从范围中删除该单元格,然后选择另一个随机单元格。 This loop is performed a total of 6 times. 此循环总共执行6次。

Upon the completion of each loop both Range.Count and Range.Address reflect the removal, but while the loop is running, it will continue to potentially select removed cells. 每个循环完成后,Range.Count和Range.Address都将反映删除,但是在循环运行时,它将继续选择删除的单元格。 I use the cell removal function elsewhere and it performs as expected. 我在其他地方使用了细胞去除功能,它的功能与预期的一样。

If both the count and the addresses for the range are showing the removal, why are those removed cells being selected? 如果范围的计数和地址都显示了删除,为什么要选择那些删除的单元格? What am I doing wrong? 我究竟做错了什么?

Code is as follows: 代码如下:

someRange = RangeToString(namesList)
    MsgBox someRange

    someDate = namesSheet.Range("F12").Value
    For i = 1 To 3
        For j = 1 To 2
            For k = 1 To namesList.Count
                Set randomName = GetRandomName(namesList)
                MsgBox randomName & " was selected for date " & someDate & " and the size of the range is: " & namesList.Count
                available = AvailabilityCheck(randomName, someDate )
                If available = True Then
                    If i = 1 And j = 1 Then
                        namesSheet.Cells(18, "E").Value = randomName
                    ElseIf i = 1 And j = 2 Then
                        namesSheet.Cells(19, "E").Value = randomName
                    ElseIf i = 2 And j = 1 Then
                        namesSheet.Cells(18, "I").Value = randomName
                    ElseIf i = 2 And j = 2 Then
                        namesSheet.Cells(19, "I").Value = randomName
                    ElseIf i = 3 And j = 1 Then
                        namesSheet.Cells(18, "K").Value = randomName
                    ElseIf i = 3 And j = 2 Then
                        namesSheet.Cells(19, "K").Value = randomName
                    End If

                    Set namesList = ExcludeCell(namesList, genSheet.Range(randomName.Address))
                    namesString = RangeToString(namesList)
                    MsgBox namesList.Address
                Exit For
                End If
            Next k
        Next j
        If i = 1 Then
            someDate = namesSheet.Range("J12").Value
        Else
            someDate = namesSheet.Range("L12").Value
        End If
    Next i
    Set FooFunction = namesList

And the function to remove the cells is: 删除单元格的功能是:

Function ExcludeCell(ByVal rngMain As Range, rngExc As Range) As Range

    Dim rngTemp     As Range
    Dim RNG         As Range

    Set rngTemp = rngMain
    Set rngMain = Nothing

    For Each RNG In rngTemp
        If RNG.Address <> rngExc.Address Then
            If rngMain Is Nothing Then
                Set rngMain = RNG
            Else
                Set rngMain = Union(rngMain, RNG)
            End If
        End If
    Next
    Set ExcludeCell = rngMain
End Function

This had to do not with the exclusion function but the random cell generator that I was using. 这不必与排除功能有关,而与我使用的随机单元生成器有关。 Since I am not dealing with a contiguous range, I needed to account for that in my random generator code. 由于我没有处理连续范围,因此我需要在随机生成器代码中加以考虑。

Here is the before: 这是之前:

Function GetRandomName(namesList As Range) As Range

    Dim randomName As Long
    randomName = Int(Rnd * namesList.Cells.Count) + 1

    Set GetRandomName = namesList.Cells(randomName)

End Function

Here is the after: 以下是:

Function GetRandomName(namesList As Range) As Range

    Dim randomCell As Integer
    Dim randomName As Range

    Do
        randomCell = Int(Rnd * namesList.Cells.Count) + 1
        Set randomName = namesList.Cells(randomCell)

        If (randomName.Parent.name = namesList.Parent.name) Then
            Dim ints As Range

            For Each cell In namesList
                Set ints = Application.Intersect(cell, randomName)
                If (Not (ints Is Nothing)) Then
                    Set GetRandomName = randomName
                    Exit Do
                End If
            Next cell
        End If
    Loop
End Function

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

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