VBA:检查每个标题是否在范围内正常

时间:2017-04-24 10:33:49

标签: excel excel-vba vba

大家好我已经开始使用VBA并且想检查我的范围的每个标题是否处于有利位置。所以第一个位置将是:“Dana wartosc”第二个:“a”等但问题是,在每个循环中它将我带到其他地方(这意味着标题是坏的)我不明白为什么,因为当我'我在RangeHolder(1)代码中检查了Debug.Print RangeHolder(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

3 个答案:

答案 0 :(得分:2)

您的Select Case语句写错了,您还应将RangeHolder视为二维(行x列)对象。 (将其视为一维赢得会导致错误,但不太可能是您要做的事情。请参阅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语法中),那么{{1将调用表达式。

答案 1 :(得分:1)

错误地编写了select语句,而且overral方法不是一个好方法,因为它会导致代码难以理解。从行RangeHolder.Select中删除所有内容并将其替换为此内容,以便您以正确的方式在代码中显示正确的值:

    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

答案 2 :(得分: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