Excel 2007 VBA If Then语句使用多个条件

时间:2013-06-19 21:14:54

标签: excel vba if-statement for-loop

我有一个多重IF条件语句,如果有的话,则返回指定的值。当我编译下面的代码时,它返回:

cust_num    company name

XX278   
XX004       Barco   
XX004   
XX278   
XX004   
XX004   
XX278   
XX278   

我的意图是让cust_num“XX278”在公司名称下返回“Barco”,因为它匹配condition2,但由于某种原因它正在跳过它并且似乎没有按照我的意图循环遍历所有行(例如,所有XX004,XX278 cust_num都应该有公司名称)。为什么我的代码不会遍历每一行?任何帮助将不胜感激。谢谢!

Dim v As Integer
Dim y As Integer

y = 0
condition1 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX004")
condition2 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX278")
condition3 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX318")

v = ActiveSheet.Rows(1).Find("customer_name", LookAt:=xlPart).End(xlDown).Count

For y = 0 To v
If condition1 Or condition2 Or condition3 Then ActiveSheet.Rows(1).Find _
    ("company name",LookAt:=xlPart).Offset(1 + v, 0) = "Barco"
Next

3 个答案:

答案 0 :(得分:1)

设置一张工作表,该工作表在列表中包含所有Cust_Num,并在其右侧显示相应的Company Name。如下图所示:

LookUp

然后,一旦你拥有了所有集合,就使用excel的VLOOKUP函数。这是我的示例公式:

=VLOOKUP(A3,LookUp!$A$1:$B$9,2,FALSE)

VLOOKUP第一部分中的A3是对行中Cust_Num的单元格引用。

LookUp!$A$1:$B$9数组是否保存您的查找值。

2会将VLOOKUP的结果定向到第二列,在这种情况下是您手动输入的Company Name

并且FALSE只会返回完全匹配。

然后我只需要拖下这个公式,我最终得到了这个:

Finished

*注意:* 如果您需要帮助获取列表中每个Cust_Num的列表,您可以执行以下操作:

选择所有Cust_Num的列表,然后在数据选项卡的功能区中,选择高级过滤器:

enter image description here

然后在窗口中:

  1. 选择Copy To Another Location
  2. 输入Copy To范围作为查找表
  3. 请务必选中Unique Records Only
  4. 的复选框

    Unique

    然后,您只需使用这些值填写相应的Company Names,然后使用前面所述的VLOOKUP

    如果首选您可以使用此方法:

    Sub AddAllNames()
    
    Call AddCompanyNameNextToCust_Num("37004", "Varco")
    Call AddCompanyNameNextToCust_Num("44278", "Varco")
    Call AddCompanyNameNextToCust_Num("44318", "Varco")
    Call AddCompanyNameNextToCust_Num("12345", "Name1")
    Call AddCompanyNameNextToCust_Num("12344", "Name1")
    Call AddCompanyNameNextToCust_Num("12346", "Name1")
    Call AddCompanyNameNextToCust_Num("98765", "Name2")
    Call AddCompanyNameNextToCust_Num("56789", "Name2")
    Call AddCompanyNameNextToCust_Num("89756", "Name2")
    
    End Sub
    
    
    Function AddCompanyNameNextToCust_Num(strCust_Num As Variant, strCompanyName As String)
    
        Dim rngCust_nums As Range, rngFoundCell As Range, rngFirstCellFound As Range
        Dim rngCust_NumsColumn As Long
        Dim boolFinished As Boolean
    
        'Get Column With header "cust_num"
        rngCust_NumsColumn = WorksheetFunction.Match("cust_num", Rows(1), 0)
    
        'Set The Search range to column from last line
        Set rngCust_nums = ActiveSheet.Columns(rngCust_NumsColumn)
    
    
        'Get the first matching value of Cust_Num (passed into sub)
        Set rngFoundCell = rngCust_nums.Find(What:=strCust_Num, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
    
        'Check to make sure a match was found/ "Not Nothing"       
        If Not rngFoundCell Is Nothing Then
    
            'Save the First Found Cell 
            Set rngFirstCellFound = rngFoundCell
    
            'Add Company Name One Column to the Right of First Found Cust_Num
            rngFoundCell.Offset(, 1).Value = strCompanyName
    
            'Start Looping a "FindNext" 
            Do While boolFinished = False
                'Set each new match into an overwriting Variable
                Set rngFoundCell = rngCust_nums.FindNext(After:=rngFoundCell)
    
                'Make sure the match is "Something"
                If Not rngFoundCell Is Nothing Then
                    'Make sure We have not gone through the whole list and that we
                    'are not back to the begining
                    If rngFoundCell.Address = rngFirstCellFound.Address Then Exit Do
                    'If a new match is found and is not the starting point then add 
                    'the company name in the next column
                    rngFoundCell.Offset(, 1).Value = strCompanyName
                Else
                    'When nothing is Found End loop
                    boolFinished = True
                End If
    
            Loop
    
        Else 'If not even one match was found
            MsgBox strCust_Num & " not Found"
        End If
    
    
    End Function
    

答案 1 :(得分:0)

在循环开始之前看起来你的条件正在被评估(即仅在y = 0时评估,而不是在循环期间评估。)

我发现你的代码存在一些问题,但你可以试试这个(没有保证,我现在无法测试):

Dim v As Integer
Dim y As Integer
Dim condition1 as boolean
Dim condition2 as boolean
Dim condition3 as boolean

y = 0

v = ActiveSheet.Rows(1).Find("customer_name", LookAt:=xlPart).End(xlDown).Count

For y = 0 To v
    condition1 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX004")
    condition2 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX278")
    condition3 = (ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart).Offset(1 + y, 0) = "XX318")
    If condition1 Or condition2 Or condition3 Then ActiveSheet.Rows(1).Find _
        ("company name",LookAt:=xlPart).Offset(1 + v, 0) = "Barco"
Next

答案 2 :(得分:0)

通过其他帖子的帮助,我找到了一种更简单的方法来评估多个条件,而不是使用If Then语句,我使用了Select Case,我发现它更有效率。下面的代码将连续查找cust_num,如果满足大小写,则公司名称将插入到右侧一列的同一行中。

Dim Nu As Range
Dim cmpny As Range
Dim v As Integer
Dim y As Integer

v = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row 'count number of rows


Set Nu = ActiveSheet.Rows(1).Find("cust_num", LookAt:=xlPart) 'set Nu = cust_num column header
Set cmpny = ActiveSheet.Rows(1).Find("company name", LookAt:=xlPart) 'set cmpny = company name column header

For y = 0 To v 'loop through each row

    Select Case Nu.Offset(1 + y, 0).Value 'row 1 + y of "cust_num"
        Case "XX004", "XX278", "XX318" 'if "cust_num" row = these #'s
            cmpny.Offset(1 + y, 0).Value = "Barco" 'Then corresponding row under "company name" column = "Varco"
Next