簡體   English   中英

僅選擇滿足條件的范圍內的第一個單元格

[英]Selecting only the first cell in a range that meets the condition

我的代碼將文本從矩陣1中的一個單元格復制到所有滿足矩陣2中我的條件的單元格中,但是我希望它僅將其復制到在矩陣2中滿足我的要求的第一個單元格中,然后停止。

Private Sub CommandButton1_Click()

Dim i As Integer
Dim j As Integer

For j = 2 To 2
    For i = 21 To 21

        If Cells(i, j).Value > 0 Then
            Cells(i, j).Value = Cells(i, j).Value - 1
            Cells(i, j).Offset(0, -1).Select
        End If

        'as it says - for EACH - so it copies in aLL the cells'
        'I can't Change the range though, cause there will come a Loop eventually'
        For Each cell In Range("a1:aap15")

            If cell.Interior.ColorIndex = 6 Then
                If cell.Value = "" Then
                    cell.Value = ActiveCell.Value
                End If
            End If
        Next
    Next
Next

End Sub

您可以使用Exit For命令退出for循環。 您似乎想在此處添加它:

If cell.Interior.ColorIndex = 6 Then
    If cell.Value = "" Then
        cell.Value = ActiveCell.Value
        Exit For
    End If
End If

注意:未經測試。 如果您有任何問題,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM