简体   繁体   English

VBA拒绝空的SpecialCells范围

[英]VBA rejecting empty Range of SpecialCells

I'm having a problem with a "For Each" related to a Range defined by a SpecialCells method. 我遇到与SpecialCells方法定义的范围相关的“ For Each”问题。 When I run the code below, there is a "Next without For" error, which I believe is because the rRange is empty when I first run the code. 当我运行下面的代码时,会出现“下一个没有For”错误,这是因为第一次运行代码时rRange为空。 I could put "On Error Resume Next" in the beggining of the sub, but I'm trying to avoid this. 我可以将“ On Error Resume Next”放在该子程序的开头,但是我试图避免这种情况。

Public Sub Sub1()

Set rRange = Worksheets("Combate").Range("69:99").SpecialCells(xlCellTypeConstants, xlNumbers)
If Not rRange Is Nothing Then
    For Each c In rRange
        If c.Value <= turnoseg Then
            c.Offset(-2 * lincomb0 + 6).Value = c.Offset(-lincomb0 + 3).Value
            c.Value = ""
        Next c
    atualizarefeitos6
    End If

End Sub

In another sub, I'm having a "No cells were selected" error after I run the code below. 在另一个子程序中,运行以下代码后出现“未选择任何单元格”错误。 I really don't know how to actually solve the errors in these subs, but you guys surely would know. 我真的不知道如何真正解决这些潜艇中的错误,但是你们一定会知道的。

Sub efeitosaddatac6()

'On Error Resume Next
Set rRange = Worksheets("Combate").Range("69:99").SpecialCells(xlCellTypeConstants, xlNumbers)
For Each c In rRange
    c.Value = c.Value + 1
    Next c
atualizarefeitos6

End Sub

Thanks in advance. 提前致谢。

As pointed out in a comment by John Coleman, your first subroutine isn't working because you are missing an End If . 正如John Coleman在评论中指出的那样,您的第一个子例程无法正常工作,因为您缺少End If You probably want: 您可能想要:

Public Sub Sub1()    
    Set rRange = Worksheets("Combate").Range("69:99").SpecialCells(xlCellTypeConstants, xlNumbers)
    If Not rRange Is Nothing Then
        For Each c In rRange
            If c.Value <= turnoseg Then
                c.Offset(-2 * lincomb0 + 6).Value = c.Offset(-lincomb0 + 3).Value
                c.Value = ""
            End If
        Next c
        atualizarefeitos6
    End If
End Sub

This is one of the reasons that consistent indentation of code is useful - it highlights missing End If s, etc. 这是一致的代码缩进有用的原因之一-突出显示缺少End If ,等等。


I would recommend you change your second subroutine as follows: 我建议您如下更改第二个子例程:

Sub efeitosaddatac6()        

    Set rRange = Nothing
    On Error Resume Next
    Set rRange = Worksheets("Combate").Range("69:99").SpecialCells(xlCellTypeConstants, xlNumbers)
    On Error GoTo 0

    If Not rRange Is Nothing Then        
        For Each c In rRange
            c.Value = c.Value + 1
        Next c
        atualizarefeitos6
    End If    
End Sub

Also, if you are not already using Option Explicit at the start of your code module, I recommend you do so. 另外,如果在代码模块开始时尚未使用Option Explicit ,则建议您这样做。 (I'm hoping that you are already using it, and that the lack of variable declarations within each subroutine is simply because they have all been declared at the module level.) (我希望您已经在使用它,并且每个子例程中缺少变量声明只是因为它们都是在模块级别上声明的。)

Luis Filho, You need to insert: 路易斯·菲略(Luis Filho),您需要输入:

     End If

before 之前

     Next c

Another item you need to define is: 您需要定义的另一项是:

     atualizarefeitos6

Is this a variable or function? 这是变量还是函数?

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

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