2017-04-24 111 views
0

你好傢伙我開始使用VBA,並且想要檢查我的範圍中的每個標頭是否處於良好位置。所以第一的位置是:「Dana wartosc」第二:「a」等等。但問題是,在每個循環中,它都把我帶到其他地方(這意味着頭部壞),我看不出爲什麼,因爲當我'在代碼中檢查RangeHolder(1)Debug.Print RangeHolder(1)它向我顯示了合適的值。VBA:檢查每個標頭是否在範圍內

你能告訴我我錯過了什麼嗎?

我想要的是它在檢查switch 1標題。該消息說,第一個報頭是不錯,它的檢查每頭在循環

enter image description here

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)) 
     RangeHolder.Select 

     For x = 1 To 4 
      Select Case RangeHolder(x) 
      Case RangeHolder(1) = "Dana wartosc" 
       MsgBox ("Its good") 
      Case RangeHolder(2) = "a" 
       MsgBox ("Its good") 
      Case RangeHolder(3) = "b" 
       MsgBox ("Its good") 
      Case RangeHolder(4) = "c" 
       MsgBox ("Its good") 
      Case Else 
       MsgBox ("Its bad" + RangeHolder(x)) 
      End Select 
     Next x 
    End Sub 
+0

目前尚不清楚你想達到什麼樣的,你可以發佈絲網你的'RangeHolder'的鏡頭?我們可以看到你正在尋找標題的'Range'? –

+0

您的'Select Case'語句將比較多個布爾表達式(例如'RangeHolder(1)=「Dana wartosc」')與RangeHolder(x)'的值。 – YowE3K

回答

2

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表達式。

+0

不錯,再次工作是在我的方式,但愛你的答案 –

+0

謝謝男人的第二個答案是我在找 – kstroz

+0

再次看到一些陣列;)在第二部分(+1),這是類似於我的張貼在同一個實例。唯一的事情可能最好在開始時只創建一次數組。 –

0

你的語法有缺陷。請試試這個: -

Sub Szukanka() 
    Dim UpRow As Long, DownRow As Long  ' rows and columns are generally Longs 
    Dim RangeHolder As Range 
    Dim x As Long       ' it's a column, right? 

    x = 1 
    UpRow = 1 
    DownRow = 5 
    Set RangeHolder = Range(Cells(UpRow, 1), Cells(UpRow, 4)) 
' RangeHolder.Select      ' don't need to select anything 

    For x = 1 To 4 
     Select Case RangeHolder(x).Value 
      Case "Dana wartosc" 
       MsgBox ("Its good") 
      Case "a" 
       MsgBox ("Its good") 
      Case "b" 
       MsgBox ("Its good") 
      Case "c" 
       MsgBox ("Its good") 
      Case Else 
       MsgBox ("Its bad" + RangeHolder(x)) 
     End Select 
    Next x 
End Sub 
1

select語句被錯誤地編寫,而且overral方法不是一個好方法,因爲它會導致可讀性差的代碼。從行RangeHolder.Select刪除所有與這個替換它,讓你的正確的價值觀在readble和oredered的方式出現在代碼:

Dim correctValues: correctValues = Array("", _ 
     "Dana wartosc", "a", "b", "c") '<-- write correct sequence here 
    For x = 1 To UBound(correctValues) 
     If RangeHolder(x) <> correctValues(x) Then 
      msgBox (RangeHolder(x) & " is not at the correct position :(") 
      Exit Sub 
     End If 
    Next x 
    msgBox "All is good :) " 
End Sub 
+1

更好:)沒有想到這種方法 –

相關問題