如何在IF语句中使用AND [英] How to use AND in IF Statement

查看:619
本文介绍了如何在IF语句中使用AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查:

IF 单元格(i,"A")包含文本迈阿密" AND (i,"D")包含文本佛罗里达" 将单元格(i,"C")的值更改为BA.

IF cells (i,"A") contains the text 'Miami' AND (i,"D") contains the text 'Florida' THEN change value of cell (i,"C") to 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

推荐答案

简要语法课

Cells(Row, Column)标识一个单元格.行必须是1到所用Excel版本的最大值之间的整数.列必须是标识符(例如:"A","IV","XFD")或数字(例如:1、256、16384)

Cells(Row, Column) identifies a cell. Row must be an integer between 1 and the maximum for version of Excel you are using. Column must be a identifier (for example: "A", "IV", "XFD") or a number (for example: 1, 256, 16384)

.Cells(Row, Column)标识先前的With语句标识的工作表中的单元格:

.Cells(Row, Column) identifies a cell within a sheet identified in a earlier With statement:

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

如果省略点,则Cells(Row,Column)在活动工作表中.因此,wsh = ActiveWorkbook wsh.Range并非绝对必要.但是,我总是使用With语句,所以我不奇怪六个月后返回代码时要使用的是哪张纸.所以,我会写:

If you omit the dot, Cells(Row,Column) is within the active worksheet. So wsh = ActiveWorkbook wsh.Range is not strictly necessary. 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. 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

因为我的代码仅适用于工作表xxxx.

because my code only works on sheet xxxx.

Cells(Row,Column)标识一个单元格. Cells(Row,Column).xxxx标识单元格的属性. Value是一个属性.值是默认属性,因此您通常可以忽略它,编译器将知道您的意思.但是在某些情况下,编译器可能会感到困惑,因此包含.Value的建议是很好的.

Cells(Row,Column) identifies a cell. Cells(Row,Column).xxxx identifies a property of the cell. Value is a property. Value is the default property so you can usually omit it and the compiler will know what you mean. But in certain situations the compiler can be confused so the advice to include the .Value is good.

Cells(Row,Column) like "*Miami*"将给出True.

Cells(Row,Column).Value = "Miami"将给出True.例如,"MIAMI"将给出False.如果要接受MIAMI,请使用小写功能:

Cells(Row,Column).Value = "Miami" will give True if the cell is exactly equal to "Miami". "MIAMI" for example will give False. If you want to accept MIAMI, use the lower case function:

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

我的建议

当您尝试不同的建议时,您的示例代码会不断变化,这些建议令我感到困惑.当我开始输入此字词时,您正在使用Cells(Row,Column) <> "Miami".

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.

使用

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".

使用

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".

使用

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

如果您不关心大小写.

这篇关于如何在IF语句中使用AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆