你Select Case
语句已被不正确地写的,你也应该治疗RangeHolder
为二维(行×列)对象。 (将其视为一维的,不会造成错误,但不太可能是你正在尝试做的。见this question为它混淆其他用户的情况。)
Option Explicit
Sub Szukanka()
Dim UpRow As Integer, DownRow As Integer, RangeHolder As Range
Dim x
x = 1
UpRow = 1
DownRow = 5
Set RangeHolder = Range(Cells(UpRow, 1), Cells(DownRow, 4))
Dim Good As Boolean
For x = 1 To 4
Good = False
Select Case RangeHolder(1, x).Value
Case "Dana wartosc"
Good = x = 1
Case "a"
Good = x = 2
Case "b"
Good = x = 3
Case "c"
Good = x = 4
End Select
If Good Then
MsgBox "It's good"
Else
MsgBox "It's bad " & RangeHolder(1, x)
End If
Next x
End Sub
我试图写上面的Select Case
做我认为你正在尝试做的事(即检查第一行,以确保它具有正确的值作为标题?)。然而,真的没有借给自己一个Select Case
结构,你会过得更好写它作为If
声明
Option Explicit
Sub Szukanka()
Dim UpRow As Integer, DownRow As Integer, RangeHolder As Range
Dim x
x = 1
UpRow = 1
DownRow = 5
Set RangeHolder = Range(Cells(UpRow, 1), Cells(DownRow, 4))
Dim CorrectValues As Variant
CorrectValues = Array("Dana wartosc", "a", "b", "c")
For x = 1 To 4
If RangeHolder(1, x).Value = CorrectValues(x - 1) Then
MsgBox "It's good"
Else
MsgBox "It's bad " & RangeHolder(1, x)
End If
Next x
End Sub
之所以你Select Case
不工作:
如果您的原始Select Case
语句改写为等价块If
声明,它会是这个样子的:
If RangeHolder(x) = (RangeHolder(1) = "Dana wartosc") Then
MsgBox ("Its good")
ElseIf RangeHolder(x) = (RangeHolder(2) = "a") Then
MsgBox ("Its good")
ElseIf RangeHolder(x) = (RangeHolder(3) = "b") Then
MsgBox ("Its good")
ElseIf RangeHolder(x) = (RangeHolder(4) = "c") Then
MsgBox ("Its good")
Else
MsgBox ("Its bad" + RangeHolder(x))
End If
如果RangeHolder(x)
值为"a"
(和RangeHolder(1)
为"Dana wartosc"
,RangeHolder(2)
是"a"
,RangeHolder(3)
是"b"
,和RangeHolder(4)
为"c"
),其然后变成:
If "a" = True Then
MsgBox ("Its good")
ElseIf "a" = True Then
MsgBox ("Its good")
ElseIf "a" = True Then
MsgBox ("Its good")
ElseIf "a" = True Then
MsgBox ("Its good")
Else
MsgBox ("Its bad" + "a")
End If
作为"a"
不是= True
(它会实际上给出类型不匹配,如果这样写,但不会在Select Case
语法中),则会调用Else
表达式。
目前尚不清楚你想达到什么样的,你可以发布丝网你的'RangeHolder'的镜头?我们可以看到你正在寻找标题的'Range'? –
您的'Select Case'语句将比较多个布尔表达式(例如'RangeHolder(1)=“Dana wartosc”')与RangeHolder(x)'的值。 – YowE3K