0

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

cust_num    company name

XX278   
XX004       Barco   
XX004   
XX278   
XX004   
XX004   
XX278   
XX278   

我的意图是让 cust_num "XX278" 在公司名称下也返回 "Barco",因为它与条件 2 匹配,但由于某种原因,它跳过了它,并且似乎没有像我预期的那样循环遍历所有行(例如,所有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
4

3 回答 3

1

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

抬头

然后,一旦您完成所有设置,请使用VLOOKUPexcel 中的函数。这是我的示例公式:

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

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

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

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

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

然后我只需要把这个公式拖下来,我就得到了这个:

完成的

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

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

在此处输入图像描述

然后在窗口中:

  1. 选择Copy To Another Location
  2. 输入Copy To范围作为查找表
  3. 确保选中复选框Unique Records Only

独特的

然后你可以只用这些值填写相应的Company Names一次并使用VLOOKUP前面描述的。

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

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
于 2013-06-25T14:08:27.927 回答
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
于 2013-06-19T22:18:58.057 回答
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
于 2013-06-25T13:12:39.367 回答