简体   繁体   English

循环VBA EXCEL列时忽略其他单元格值

[英]Ignore other cell value's when looping a column VBA EXCEL

I'm going to start my problem with an example, as otherwise it will be too difficult to explain. 我将以一个示例开始我的问题,否则将很难解释。

         A        B        C           D         E (ID)
1      word              letter                  1
       test              blabla                  
       other          
2      word              letter                  2
                         number
3      test              true                    3
4      other             false                   4
5      word              letter       Yes        5
6      word              letter       Yes        6
7      test              letter                  7

What's the goal? 目标是什么?

If I'll try to explain the goal in words, it will be hard to understand; 如果我要用语言解释目标,将很难理解。 it's also hard to explain :) Anyway I also added my code, and if you are good with VBA you will understand the code better then the text. 这也很难解释:)无论如何,我也添加了我的代码,如果您对VBA很好,那么您将比文本更了解代码。

IF a value (in this example: word) is also in other rows THEN we needs to check column C, AND IF there is a value in column C that we find more then once in column C (in this example: letter), we needs to recheck if in column A the value also appears more then once. 如果在其他行中也有一个值(在此示例中:word),那么我们需要检查C列,并且如果在C列中存在一个值,然后在C列中找到一个值(在本示例中为letter),那么我们需要重新检查A列中的值是否还会出现一次。

So I already made a SUB, AND IT WORKS ! 因此,我已经制作了一个SUB,它可以正常工作! :) BUT not if there are more values in a cell. :)但如果单元格中有更多值,则不会。 So in the example when there is only 1 value in a cell, as in row 6 & 7, column D returns : YES 因此,在示例中,当单元格中只有1个值时(如第6和7行),D列返回:YES

Here is my code so far. 到目前为止,这是我的代码。

Sub duplicates()

Dim source As Range
Dim source2 As Range

For Each source In Range("A1", Range("A" & Rows.Count).End(xlUp))
 If source.Value <> "" Then
    For Each source2 In Range("A1", Range("A" & Rows.Count).End(xlUp))


    If source.Value = source2.Value And source.Offset(0, 4).Value <> source2.Offset (0, 4).Value Then

        If source.Offset(0, 2).Value = source2.Offset(0, 2).Value Then

         source.Offset(0, 3) = "Yes"
        End If
    End If
    Next source2
  End If
 Next source
End Sub

So, we should return: YES in row 1 & 2 as well. 因此,我们应该在第1行和第2行返回:YES。 Hope you understand my goal. 希望你理解我的目标。 Hope someone can help. 希望有人能帮忙。

My proposal is as follows: 我的建议如下:

A) additional function would check each element of each cell passed to the function as array: A)附加函数将检查作为数组传递给函数的每个单元格的每个元素:

Function AnyEqual(ColA, ColB) As Boolean

Dim itemA, itemB
For Each itemA In ColA
    For Each itemB In ColB
    If itemA = itemB Then
        AnyEqual = True
        Exit Function
    End If
    Next
Next

End Function

B) Some changes are made in your code - put it inside For Each source2 loop instead your inner code: B)对您的代码进行了一些更改 -将其放在For Each source2循环中,而不是您的内部代码中:

If AnyEqual(Split(source, Chr(10)), Split(source2, Chr(10))) And _
    source.Offset(0, 4).Value <> source2.Offset(0, 4).Value Then

    If AnyEqual(Split(source.Offset(0, 2), Chr(10)), _
                   Split(source2.Offset(0, 2), Chr(10))) Then

        source.Offset(0, 3) = "Yes"
    End If
End If

Based on data you provided it seems it works fine. 根据您提供的数据,看来效果很好。 I hope it is what you were looking for as it was a bit complicated to understand your needs. 我希望这是您想要的,因为了解您的需求有点复杂。

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

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