VBA:检查每个头是否在范围内罚款
你好,我开始在VBA,并要检查我的范围内的每个标题是否处于良好的位置。 所以第一的位置是:“Dana wartosc”第二:“a”等等。但问题是,在每一个循环中,都把我带到别的地方(这意味着头是坏的),我不明白为什么, m检查RangeHolder(1)
通过Debug.Print RangeHolder(1)
在代码中它显示我适当的价值。
你能告诉我我错过了什么吗?
我想要的是当它在检查开关第一个标题。 消息是说,第一个头是好的,它是在循环检查每个头
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
您的Select Case
语句被错误地写入了,您应该将RangeHolder
当作一个二维(行x列)对象。 (将其视为一维不会造成错误,但不太可能成为您想要做的事情。请参阅此问题以解决混淆其他用户的情况。)
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
(如果这样写,实际上会产生types不匹配,但不会在Select Case
语法中),则会调用Else
expression式。
select语句被错误地编写,而且overral方法也不是一个好方法,因为它会导致代码的可读性差。 删除RangeHolder.Select
所有行,并将其replace为这样,以便您的正确值以可读方式出现在代码中:
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
你的语法有缺陷。 请试试这个:
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