我的VBA IF语句正在生成一个编译错误



我的代码有问题,我似乎找不到它,所以我需要另一双眼睛——我想我要失明了。

这是我的代码,非常简单,如果有人在做更高级别的工作,那么3列将变灰,n/a,因为他们不会得到D、F和G列模块的任何结果。如果没有做更高的级别,那么情况将相反,他们不会得到B、C和E列模块的结果。共有28名受训人员,因此需要对所有行都有效。

当前代码如下:

Private Sub Update()
Dim Course As String
Dataset = Range("A").Value
If Course = "Higher" Then
Range("D").Value = "N/A"
Range("F").Value = "N/A"
Range("G").Value = "N/A"
Range("D,F,G").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
Else
Range("B").Value = "N/A"
Range("C").Value = "N/A"
Range("E").Value = "N/A"
Range("B,C,E").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End If
End With
End Sub

然而,我得到了一个编译错误,说";Else不带和If";。据我所知,所有变量都已声明,IF语句的结构是正确的。

有什么想法吗?

试试这个:

Private Sub Update()
Dim Course As String
Dataset = Range("A").Value
If Course = "Higher" Then
Range("D").Value = "N/A"
Range("F").Value = "N/A"
Range("G").Value = "N/A"
Range("D,F,G").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Else
Range("B").Value = "N/A"
Range("C").Value = "N/A"
Range("E").Value = "N/A"
Range("B,C,E").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
End If
End Sub

如果归结为:

If ... Then
...
With ...     'Start the first With-clause
...
End With     'End the first With-clause
Else           'All With-clauses need to be closed before you can go here
...
With ...     'Start the second With-clause
...
End With     'End the second With-clause
End If         'All With-clauses needed be closed before you can go here.

您有2个With语句,只有1个End With

With Selection.Interior移动到第一个If的上方,然后移除第二个With

将最后一个End With移动到End If之上。

相关内容

最新更新