简体   繁体   English

如何在IF语句中使用AND

[英]How to use AND in IF Statement

I want to check: 我想查一下:

IF cells (i,"A") contains the text 'Miami' AND (i,"D") contains the text 'Florida' THEN change value of cell (i,"C") to BA. IF单元(i,“A”)包含文本'Miami' (i,“D”)包含文本' Florida'THEN将单元格(i,“C”)更改为BA。

Sub ABC()
Dim wsh As Worksheet, i As Long, lngEndRowInv As Long
Set wsh = ActiveSheet

i = 2
lngEndRowInv = wsh.Range("A" & wsh.Rows.Count).End(xlUp).Row
While i <= lngEndRowInv
    If Cells(i, "A") like "*Miami*" And Cells(i, "D") like "*Florida*" Then
        Cells(i, "C").Value = "BA"
    End If
    i = i + 1
Wend
End Sub

Brief syntax lesson 语法课程简短

Cells(Row, Column) identifies a cell. Cells(Row, Column)标识单元格。 Row must be an integer between 1 and the maximum for version of Excel you are using. 行必须是1到您正在使用的Excel版本的最大值之间的整数。 Column must be a identifier (for example: "A", "IV", "XFD") or a number (for example: 1, 256, 16384) 列必须是标识符(例如:“A”,“IV”,“XFD”)或数字(例如:1,256,16384)

.Cells(Row, Column) identifies a cell within a sheet identified in a earlier With statement: .Cells(Row, Column)标识在早期With语句中标识的工作表中的单元格:

With ActiveSheet
  :
  .Cells(Row,Column)
  :
End With

If you omit the dot, Cells(Row,Column) is within the active worksheet. 如果省略点,则Cells(Row,Column)位于活动工作表中。 So wsh = ActiveWorkbook wsh.Range is not strictly necessary. 所以wsh = ActiveWorkbook wsh.Range不是绝对必要的。 However, I always use a With statement so I do not wonder which sheet I meant when I return to my code in six months time. 但是,我总是使用With语句,所以当我在六个月后返回我的代码时,我不知道我的意思。 So, I would write: 所以,我会写:

With ActiveSheet
  :
  .Range.  
  :
End With

Actually, I would not write the above unless I really did want the code to work on the active sheet. 实际上,除非我真的希望代码在活动工作表上工作,否则我不会写上面的内容。 What if the user has the wrong sheet active when they started the macro. 如果用户在启动宏时激活了错误的工作表,该怎么办? I would write: 我会写:

With Sheets("xxxx")
  :
  .Range.  
  :
End With

because my code only works on sheet xxxx. 因为我的代码仅适用于工作表xxxx。

Cells(Row,Column) identifies a cell. Cells(Row,Column)标识单元格。 Cells(Row,Column).xxxx identifies a property of the cell. 单元格(行,列).xxxx标识单元格的属性。 Value is a property. Value是一种财产。 Value is the default property so you can usually omit it and the compiler will know what you mean. Value是默认属性,因此您通常可以省略它,编译器将知道您的意思。 But in certain situations the compiler can be confused so the advice to include the .Value is good. 但在某些情况下编译器可能会混淆,因此包含.Value的建议是好的。

Cells(Row,Column) like "*Miami*" will give True if the cell is "Miami", "South Miami", "Miami, North" or anything similar. 如果单元格是“迈阿密”,“南迈阿密”,“迈阿密,北方”或类似的单元格,那么Cells(Row,Column) like "*Miami*"将给出True。

Cells(Row,Column).Value = "Miami" will give True if the cell is exactly equal to "Miami". 如果单元格完全等于“迈阿密”,则单元Cells(Row,Column).Value = "Miami"将给出True。 "MIAMI" for example will give False. 例如,“迈阿密”将给予假。 If you want to accept MIAMI, use the lower case function: 如果您想接受MIAMI,请使用小写功能:

Lcase(Cells(Row,Column).Value) = "miami"  

My suggestions 我的建议

Your sample code keeps changing as you try different suggestions which I find confusing. 当您尝试不同的建议时,您的示例代码会不断变化 You were using Cells(Row,Column) <> "Miami" when I started typing this. 当我开始输入时Cells(Row,Column) <> "Miami"您正在使用Cells(Row,Column) <> "Miami"

Use 采用

If Cells(i, "A").Value like "*Miami*" And Cells(i, "D").Value like "*Florida*" Then
  Cells(i, "C").Value = "BA"

if you want to accept, for example, "South Miami" and "Miami, North". 如果你想接受,例如,“南迈阿密”和“迈阿密,北”。

Use 采用

If Cells(i, "A").Value = "Miami" And Cells(i, "D").Value like "Florida" Then
  Cells(i, "C").Value = "BA"

if you want to accept, exactly, "Miami" and "Florida". 如果你想接受“迈阿密”和“佛罗里达”。

Use 采用

If Lcase(Cells(i, "A").Value) = "miami" And _
   Lcase(Cells(i, "D").Value) = "florida" Then
  Cells(i, "C").Value = "BA"

if you don't care about case. 如果你不关心案件。

If there are no typos in the question, you got the conditions wrong: 如果问题中没有拼写错误,则说明条件错误:

You said this: 你说的这个:

IF cells (i,"A") contains the text 'Miami' IF单元格(i,“A”)包含文本'Miami'

...but your code says: ...但你的代码说:

If Cells(i, "A") <> "Miami"

--> <> means that the value of the cell is not equal to "Miami", so you're not checking what you think you are checking. - > <>表示单元格的值不等于 “迈阿密”,因此您不会检查您认为正在检查的内容。

I guess you want this instead: 我想你想要这个:

If Cells(i, "A") like "*Miami*"

EDIT: 编辑:

Sorry, but I can't really help you more. 对不起,但我真的无法帮助你。 As I already said in a comment, I'm no Excel VBA expert. 正如我在评论中已经说过的那样,我不是Excel VBA专家。
Normally I would open Excel now and try your code myself, but I don't even have Excel on any of my machines at home (I use OpenOffice). 一般情况下我现在打开Excel和尝试你的代码自己,但我甚至不具备的Excel在家里(我使用OpenOffice)我的任何机器。

Just one general thing: can you identify the row that does not work? 只是一个普遍的事情:你能识别出不起作用的行吗?
Maybe this helps someone else to answer the question. 也许这有助于其他人回答这个问题。

Does it ever execute (or at least try to execute) the Cells(i, "C").Value = "BA" line? 它是否曾执行(或至少尝试执行) Cells(i, "C").Value = "BA"行?
Or is the If Cells(i, "A") like "*Miami*" stuff already False ? 或者If Cells(i, "A") like "*Miami*"这样的If Cells(i, "A") like "*Miami*"已经False吗?
If yes, try checking just one cell and see if that works. 如果是,请尝试仅检查一个单元格,看看是否有效。

If you are simply looking for the occurrence of "Miami" or "Florida" inside a string (since you put * at both ends), it's probably better to use the InStr function instead of Like. 如果您只是在字符串中寻找“迈阿密”或“佛罗里达”的出现(因为您在两端都放了*),最好使用InStr函数而不是Like。 Not only are the results more predictable, but I believe you'll get better performance. 结果不仅更可预测,而且我相信你会获得更好的表现。

Also, VBA is not short-circuited so when you use the AND keyword, it will test both sides of the AND, regardless if the first test failed or not. 此外,VBA没有短路,因此当您使用AND关键字时,它将测试AND的两侧,无论第一次测试是否失败。 In VBA, it is more optimal to use 2 if-statements in these cases, that way you aren't checking for "Florida" if you don't find "Miami". 在VBA中,在这些情况下使用2个if语句更为理想,如果您没有找到“Miami”,则不会检查“Florida”。

The other advice I have is that a for-each loop is faster than a for-loop. 我的另一个建议是for-each循环比for循环更快。 Using .offset, you can achieve the same thing, but with better effeciency. 使用.offset,您可以实现相同的目标,但效率更高。 Of course there are even better ways (like variant arrays), but those will add a layer of complexity not needed in this example. 当然,还有更好的方法(如变体数组),但这些方法会增加本例中不需要的复杂层。

Here is some sample code: 以下是一些示例代码:

Sub test()

Application.ScreenUpdating = False
Dim lastRow As Long
Dim cell As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row

For Each cell In Range("A1:A" & lastRow)
    If InStr(1, cell.Value, "Miami") <> 0 Then
        If InStr(1, cell.Offset(, 3).Value, "Florida") <> 0 Then
            cell.Offset(, 2).Value = "BA"
        End If
    End If
Next

Application.ScreenUpdating = True
End Sub

I hope you find some of this helpful, and keep at it with VBA! 我希望你能找到一些有用的东西,并与VBA保持联系! ^^ ^^

I think you should append .value in IF statement: 我认为你应该在IF语句中附加.value:

If Cells(i, "A").Value <> "Miami" And Cells(i, "D").Value <> "Florida" Then
    Cells(i, "C").Value = "BA"
End IF

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

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