结束如果无障碍如果错误 [英] End If without block If error

查看:48
本文介绍了结束如果无障碍如果错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码.我不明白为什么会出现错误:End If if block if

  Sub mytest()昏暗的整数昏暗的整数Dim j作为整数昏暗p作为整数Dim k作为整数s = 0使用工作表("mysheet").Range("B28:B75").选择对于我= 28至75如果单元格(i,2).值>0然后单元格(i,2).Interior.Color = RGB(255,255,255)s = s + 1万一接下来我.Range("A28:A75").Select对于j = 28到75如果Cells(i,2).Value = 0,则Cells(i,2).Interior.Pattern = xlPatternLightDown单元格(i,2).Interior.Color = RGB(255,255,255)万一下一个jp = 75-s对于k = 1至s单元格(s + k,1).Interior.Color = RGB(18,0,0)下一个k结束于 

没有省略End If子句.我不明白为什么会出错

解决方案

在新行的 Then 之后移动语句.IMHO VBA如果在此之后将其与代码一起使用,则期望单行语句,因此如果代码没有 end 则下一行代码会抛出此错误.我还建议您在此处阅读更多有关VBA的信息,以获得更好的范围和单元格规范./p>

但是对于您的错误,请使用类似的

  Sub mytest()昏暗的整数昏暗的整数Dim j作为整数昏暗p作为整数Dim k作为整数s = 0使用工作表("mysheet").Range("B28:B75").选择对于我= 28至75如果单元格(i,2).值>0然后单元格(i,2).Interior.Color = RGB(255,255,255)s = s + 1万一接下来我.Range("A28:A75").Select对于j = 28到75如果Cells(i,2).Value = 0然后单元格(i,2).Interior.Pattern = xlPatternLightDown单元格(i,2).Interior.Color = RGB(255,255,255)万一下一个jp = 75-s对于k = 1至s单元格(s + k,1).Interior.Color = RGB(18,0,0)下一个k结束于 

I Wrote this code. I dont understand why I got error : End If without block If

Sub mytest()

Dim i As Integer
Dim s As Integer
Dim j As Integer
Dim p As Integer
Dim k As Integer
s = 0

With Worksheets("mysheet")
.Range("B28:B75").Select

For i = 28 To 75
        If Cells(i, 2).Value > 0 Then Cells(i, 2).Interior.Color = RGB(255, 255, 255)
        s = s + 1
        End If
Next i

.Range("A28:A75").Select

For j = 28 To 75

    If Cells(i, 2).Value = 0 Then Cells(i, 2).Interior.Pattern = xlPatternLightDown
    Cells(i, 2).Interior.Color = RGB(255, 255, 255)
    End If

Next j

p = 75 - s
For k = 1 To s
    Cells(s + k, 1).Interior.Color = RGB(18, 0, 0)
Next k

End With

The End If clause wasnt omitted. I dont understand why I got error

解决方案

Move statements after Then on new line. IMHO VBA expects single line statement if you use it with code after then, so next line with code without end if throws this error. I also recomends you to read more about VBA here for better range and cell specification.

but for your error use something like this

Sub mytest()

Dim i As Integer
Dim s As Integer
Dim j As Integer
Dim p As Integer
Dim k As Integer
s = 0

With Worksheets("mysheet")
.Range("B28:B75").Select

For i = 28 To 75
        If Cells(i, 2).Value > 0 Then
        Cells(i, 2).Interior.Color = RGB(255, 255, 255)
        s = s + 1
        End If
Next i

.Range("A28:A75").Select

For j = 28 To 75

    If Cells(i, 2).Value = 0 Then
    Cells(i, 2).Interior.Pattern = xlPatternLightDown
    Cells(i, 2).Interior.Color = RGB(255, 255, 255)
    End If

Next j

p = 75 - s
For k = 1 To s
    Cells(s + k, 1).Interior.Color = RGB(18, 0, 0)
Next k

End With

这篇关于结束如果无障碍如果错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆