VBA不承认“如果”陈述条件
我想在Excel VBA中使用“If”逻辑,但脚本始终select两个区域的第一个选项,即使语句不正确。
代码的要点在于如果单词“Rate”出现在屏幕上的一个位置,那么应该创build某个输出。 如果发现它在不同的位置,则应创build不同的输出。 目前,即使第一个陈述是不正确的,它已经准备好了第一个“If”陈述和拉动输出。
使用下面的例子,屏幕上的第(4,20)行显示“Rate”这个词,但是这个语句创build的输出就像它在(4,16)处一样,因此把输出从(4,27) (4,31)。 我已经改变了顺序,它总是使用第一个选项,不pipe它是否是正确的语句。
我尽可能地缩写了代码。 我曾经Dim'd作为一个string,但改变RateName到变种,看看是否会解决这个问题,但事实并非如此。
Dim RateName As Variant Dim Curr As String If Session.FindText("Rate", 4, 16, 4) Then RateName = Trim(Session.GetDisplayText(4, 27, 20)) 'R name for Amt Add, Amt Off, Buy/Get ElseIf Session.FindText("Rate", 4, 20, 4) Then RateName = Trim(Session.GetDisplayText(4, 31, 20)) 'R name for Flat Amount, Pct Off End If If Session.FindText("Rate", 4, 16, 4) Then Curr = Trim(Session.GetDisplayText(4, 58, 3)) ElseIf Session.FindText("Rate", 4, 20, 4) Then Curr = Trim(Session.GetDisplayText(4, 62, 3)) End If
不知道我做错了什么,因为我在其他地方有类似的逻辑,它正在工作。
在VBA的情况下,返回不同的值为0不是TRUE。 当你使用某个操作符时,你会变得真实
If Session.FindText("Rate", 4, 16, 4) = "some text/value" Then If Session.FindText("Rate", 4, 16, 4) <> "some text/value" Then If Session.FindText("Rate", 4, 16, 4) >= "some text/value" Then If Session.FindText("Rate", 4, 16, 4) <= "some text/value" Then
此外,如果返回的文本是"True"
或"False"
,则VBA中的IF
只是一个文本,而不是TRUE
或FALSE
编辑#1,因为你的评论…
你可以使用这个:
If Session.FindText("Rate", 4, 16, 4) = Empty Then 'some code if the logical test is true else 'some code if the logical test is false end if