退出如果和下一步

时间:2019-02-06 17:36:36

标签: excel vba

我有一个带if语句的for next循环。如果“ If语句”为true,则退出if和next for。这是代码:我想要的是如果Rng1在Range1中不带,则转到下一个rng1。我似乎是一个简单的解决方案,但我无法弄清楚。预先谢谢你。

Sub me_test()

Dim Range1 As Range
Dim Rng1 As Range
Set Range1 = Application.Selection
Set Range1 = Application.InputBox("Please Select Your Range :", xtitledID, Range1.Address, Type:=8)

    For Each Rng1 In Range1
        If Intersect(Rng1, Range("B7:B15")) Is Nothing Then
            MsgBox "Not within the perscribed range." & vbCr & "Please click OK to continue."
        Else
            Rng1.Value = "Good"
        End If
    Next

MsgBox "All Done"

End Sub

4 个答案:

答案 0 :(得分:1)

Sub me_test()

    Dim Range1 As Range
    Dim Rng1 As Range

    Set Range1 = Application.Selection

    Set Range1 = Application.InputBox("Please Select Your Range :", xtitledID, _
                                       Range1.Address, Type:=8)

    Set Range1 = Application.Intersect(Range1, ActiveSheet.Range("B7:B15"))

    If Range1 is nothing then
         MsgBox "No valid cells selected!"
    else
         Range1.Value = "Good"
    end if 

End Sub

答案 1 :(得分:0)

这应该有效。它对我有效

Sub me_test()

    Dim Range1 As Range
    Dim Rng1 As Range
    Dim rngIntr As Range

    Set Range1 = Application.Selection
    Set Range1 = Application.InputBox("Please Select Your Range :", xtitledID, Range1.Address, Type:=8)


    For Each Rng1 In Range1
        Set rngIntr = Intersect(Rng1.Cells, Range("B7:B15").Cells)
        If rngIntr Is Nothing Then
            MsgBox "Not within the perscribed range." & vbCr & "Please click OK to continue."
        Else
            Rng1.Value = "Good"
        End If
        Set rngInr = Nothing
    Next

    Set Range1 = Nothing
    Set Rng1 = Nothing

    MsgBox "All Done"

End Sub

答案 2 :(得分:0)

这是怎么回事:如果所选单元格的都在所需范围内,则仅显示“不在...内”消息框。否则,请继续进行选择,并在与所需范围相交的每个单元格中写出 Good

Sub me_test2()

Dim Range1 As Range
Dim Rng1 As Range
Set Range1 = Application.Selection
Set Range1 = Application.InputBox("Please Select Your Range :", xtitledID, Range1.Address, Type:=8)

If Intersect(Range("B7:B15"), Range1) Is Nothing Then
    MsgBox "Not within the perscribed range." & vbCr & "Please click OK to continue."
Else
    For Each Rng1 In Range1
        If Not Intersect(Rng1, Range("B7:B15")) Is Nothing Then
            Rng1.Value = "Good"
        End If
    Next
    MsgBox "All Done"
End If

End Sub

答案 3 :(得分:0)

也许:

Sub me_test()
    Dim rng1 As Range

    Set rng1 = Application.InputBox("Please Select Your Range :", xtitledID, Selection.Address, Type:=8)

    If Union(rng1, Range("B7:B15")).Address =  Range("B7:B15").Address Then 
        rng1.Value = "Good"
    Else
        MsgBox "Not within the perscribed range." & vbCr & "Please click OK to continue."
    End If

    MsgBox "All Done"

End Sub

您可能还需要检查有效的已用范围选择:

Sub me_test()
    Dim rng1 As Range

    Do 
        Set rng1 = Application.InputBox("Please Select Your Range :", xtitledID, Selection.Address, Type:=8)
    Loop While rng1 Is Nothing

    If Union(rng1, Range("B7:B15")).Address =  Range("B7:B15").Address Then 
        rng1.Value = "Good"
    Else
        MsgBox "Not within the perscribed range." & vbCr & "Please click OK to continue."
    End If

    MsgBox "All Done"

End Sub