2017-04-24 109 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

更好:)没有想到这种方法 –

相关问题