你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