简体   繁体   English

使用Excel VBA AND OR逻辑运算符晕眩

[英]Dizzy with Excel VBA AND OR logical operators

I would like my procedure delete any row in which column a (cA) is Fund ID 6347 OR 6349 AND has a basis point impact of less than +/-100 AND less than +/-50% price change 我想我的程序删除任何行中column a (cA) is Fund ID 6347 OR 6349 ,并具有一个基点的影响less than +/-100 less than +/-50% price change

The following code works to delete rows with: either fund and a basis point impact +/- 100 but I don't believe it is working according to my intentions, as amid the extremely confusing parentheses in combination with the poor 2007 excel IDE interface, when I remove the cells(i, cJ) >= -100) code in the first line my test fails, leading me to believe that in some way my code works in part but on accident. 以下代码可删除以下行: either fund and a basis point impact +/- 100但由于括号令人困惑,加上糟糕的2007 excel IDE界面,我不认为它按我的意图工作,当我在第一行中删除cells(i, cJ) >= -100)代码时,我的测试失败了,这使我相信我的代码在某种程度上可以工作,但偶然。

Does anyone understand the logic I am trying to code? 有谁了解我尝试编写的逻辑? Is there some web GUI were I can code excel VBA and see parenthesis highlighted for a better experience in these situations? 我可以使用excel VBA编写一些Web GUI并看到括号突出显示以在这些情况下获得更好的体验吗? Thank you 谢谢

        For i = LR To 2 Step -1

                     If (Left(CStr(cells(i, cA)), 4) = "6347" And (cells(i, cJ) <= 100 And cells(i, cJ) >= -100)) Or _
                     (Left(CStr(cells(i, cA)), 4) = "6349" And (cells(i, cJ) <= 100) And cells(i, cJ) >= -100) Or _
                     Left(CStr(cells(i, cC)), 4) = "Trs " Or _
                     Mid(CStr(cells(i, cC)), 15, 8) = "bcomf1t " Or _
                     Mid(CStr(cells(i, cC)), 15, 7) = "bcomtr " Then

Pull the variables out of the spreadsheet before you build your if statement, then put them one on each line. 构建if语句之前,将变量从电子表格中拉出,然后将它们放在每一行中。 This is much more efficient and makes it much, much easier to read. 这样效率更高,而且阅读起来也容易得多。 Following your specification statement: 按照您的规格说明:

I would like my procedure delete any row in which column a (cA) is Fund ID 6347 OR 6349 AND has a basis point impact of less than +/-100 AND less than +/-50% price change 我希望我的程序删除其中(cA)列为基金ID 6347或6349并且基点影响小于+/- 100和小于+/- 50%的任何行

It would look like this (note that I'm guessing on what values come from where, and your code has additional conditions you don't specify): 它看起来像这样(请注意,我正在猜测值来自何处,并且您的代码还有未指定的其他条件):

Dim FundId As String
FundId = Left$(Cells(i, cA).Value, 4)
Dim BasisImpact As Long
BasisImpact = Cells(i, cJ).Value
Dim PriceChange As Long
PriceChange = Cells(i, whereever).Value

If (FundId = "6347" Or FundId = "6349") And _
   (BasisImpact >= -100 And BasisImpact <= 100) And _
   (PriceChange >= -50 And PriceChange <= 50) Then

As pointed out by @Jeeped in the comments, VBA will perform all of the tests in an If statement (it doesn't short circuit). 正如@Jeeped在评论中指出的那样,VBA将在If语句中执行所有测试(它不会短路)。 If this is in a performance critical part of your code or you have a huge data set, nesting the If statements and Worksheet accesses will give you better performance: 如果这是代码的性能关键部分,或者您的数据集很大,则嵌套If语句和Worksheet访问将为您提供更好的性能:

If FundId = "6347" Or FundId = "6349" Then
    Dim BasisImpact As Long
    BasisImpact = Cells(i, cJ).Value
    If BasisImpact >= -100 And BasisImpact <= 100 Then
        Dim PriceChange As Long
        PriceChange = Cells(i, whereever).Value
        If PriceChange >= -50 And PriceChange <= 50 Then
            'Do your thing
        End If
    End If
End If

If you only bother about the "fund" and "impact" check, then go this way: 如果您只为“资金”和“影响”检查而烦恼,请按照以下方式操作:

Dim okFund As Boolean, okImpact As Boolean

For I = LR To 2 Step -1

    okFund= Left(CStr(Cells(I, cA)), 4) = "6347" Or (Left(CStr(Cells(I, cA)), 4) = "6349"
    okImpact = Cells(I, cJ) <= 100 And Cells(I, cJ) >= -100

    If okFund And okImpact Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM