For each cell in a range, if a value in a seperate range is found in the next cell do stuff

僤鯓⒐⒋嵵緔 提交于 2020-01-04 05:27:22

问题


This Macro I have built works but I am hoping for a faster version or a Formula that will do the same in less time.

What I Have:

    For Each cell In Range("Table_Query_1[[#Data],[Reason2]]")
        For Each PossibleValue In Range("F2", Range("F2").End(xlDown))
            If Len(cell) = 0 Then
             If (InStr(UCase(cell.Offset(0, 1)), UCase(PossibleValue)) <> 0) Then
               cell.Value = PossibleValue.Value
             End If
             Else
                Exit For
             End If
         Next
         If Len(cell) = 0 Then
            cell.Value = cell.Offset(0, -1)
        End If
    Next

The only other way I could get anything to work way with the following Array Formula

=IF(ISNA(MATCH($F$3:$F$10,[@Extra Info],0)),[@Reason],$F$3:$F$10)

but this doesn't work for Partial matches as in the case of Row 4 and 9. I also have my doubts that this array formula would be that much faster then a vba macro along with the fact it would also require more upkeep with the test values range (F2:f3) in this case as I would have to constantly update that formula OR I wouild have to make the original range like F2:F100 witch would cause it to take that much longer.

So, what i'd like is if ANY value in my range of values (F2:F3 in this case), Is found inside of the Extra Info Column on the current Row , Then Reason2 of that row (Offset(0, -1)) equals the Value that was matched. But if nothing is found then just use the Reason in that row(Offset(0,1)).

And the second Issue is that I need the Macro to Run After the QueryTable refreshes but if I set it as a Cell Change Event on a cell the is in the query that will change, the macro runs and finishes before the Final querytable is imported and sorted.


回答1:


Solved!

This is post the comment that I posted above which had the initial formula.

=IF(COUNT(FIND($F$2:$F$3,C1)),"What Will Go Here",A1)

The below tells you what has to go in place of "What Will Go Here"

Put this formula in cell B2. Note that this is an Array Formula. You will have to press CTRL + SHIFT + ENTER after you enter the formula.

=IF(COUNT(FIND($F$2:$F$4,C2)),INDEX($F$2:$F$4,MATCH(SUM(IF(ISNUMBER(--FIND($F$2:$F$4,C2,1)),--FIND($F$2:$F$4,C2,1))),FIND($F$2:$F$4,C2,1),0),0),A2)

Screenshot

Explanation:

FIND($F$2:$F$4,C2,1) when used with an array returns an array. To check the values you can highlight it and press F9 and it will tell you the position at which the match is found. See this screenshot

So it tells us that it found the match at the 3rd position in 4532. It yet doesn't tell us with what did it find a match.

Now the next step is to retrieve the position of that number from the array. So in the above example it will be position 2 and to find that position we will use MATCH() and to use MATCH we will need that 3

So to retrieve 3 from the array we use this formula

=SUM(IF(ISNUMBER(--FIND($F$2:$F$4,C2,1)),--FIND($F$2:$F$4,C2,1)))

Now we have that 3 so we will use it in Match to find the position in the Possible Value

=MATCH(SUM(IF(ISNUMBER(--FIND($F$2:$F$4,C2,1)),--FIND($F$2:$F$4,C2,1))),FIND($F$2:$F$4,C2,1),0)

This will give us 2

Now we know the position of the number in the Possible Value. To find that number we will use INDEX

=INDEX($F$2:$F$4,MATCH(SUM(IF(ISNUMBER(--FIND($F$2:$F$4,C2,1)),--FIND($F$2:$F$4,C2,1))),FIND($F$2:$F$4,C2,1),0),0)

SAMPLE Workbook

http://wikisend.com/download/280280/Sample.xlsx




回答2:


This is a solution I came across that does not have to be Array Entered and seems to run faster then Siddharth Rout's solution. I am using the formula

=IFERROR(LOOKUP(1E+100,SEARCH($F$2:$F$4,C2),$F$2:$F$4),A2)

Where I am looking for any word in C2 that is in the range F2:F4. If none found it will throw an ERROR and in that situation I know nothing was found and simply return the original reason.

Not shown in the picture I also turn F2:F4 into a named range called Reasons and change the formula too:

    =IFERROR(LOOKUP(1E+100,SEARCH(Reasons,C2),Reasons),A2)



来源:https://stackoverflow.com/questions/16066123/for-each-cell-in-a-range-if-a-value-in-a-seperate-range-is-found-in-the-next-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!