对于一个范围内的每个单元格,如果在下一个单元格中找到一个单独的范围内的值,那么可以做到这一点 [英] For each cell in a range, if a value in a seperate range is found in the next cell do stuff

查看:148
本文介绍了对于一个范围内的每个单元格,如果在下一个单元格中找到一个单独的范围内的值,那么可以做到这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个宏我已经构建了作品,但我希望能够在更短的时间内执行更快的版本或公式。

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.

我有什么:

    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)

不适用于第4和第9行的部分比赛。我也怀疑这个数组公式将是那么一个vba宏的速度要快得多,在这种情况下,它也需要更多的维护与测试值范围(F2:f3),因为我必须不断更新该公式或者我需要使原始范围像F2 :F100巫婆会使它花费更长的时间。

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.

所以,我想要的是如果我的值范围内的任何值(在这种情况下为F2:F3),都在Extra当前行中的信息列,然后该行的Reason2(偏移量(0,-1))等于匹配的值。但是如果没有发现,那么只需使用该行中的原因(Offset(0,1))。

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

而第二个问题是我需要运行宏QueryTable刷新,但如果将其设置为单元格上的单元格更改事件,则查询中将更改该宏,宏将在导入和排序Final querytable之前运行并完成。

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.

推荐答案

解决!

这是发布我上面发布的注释,其中包含初始公式。

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

= IF(COUNT(FIND($ F $ 2:$ F $ 3,C1)),什么会去这里,A1)

下面告诉你要取代什么会去这里

将此公式放在单元格B2中。请注意,这是一个数组公式。输入公式后,您必须按 CTRL + SHIFT + ENTER

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:$ 4 F $,C2,1))),FIND($ F $ 2:$ F $ 4 C2,1),0), 0),A2)

截图

说明

FIND($ F $ 2:$ F $ 4,C2,1)数组返回一个数组。要检查值,您可以突出显示它,并按 F9 ,它会告诉你找到匹配的位置。看到这个截图

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

所以它告诉我们,它在4532年的第三名发现了这个比赛。但是它没有告诉我们找到了什么匹配。

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.

下一步是从数组中检索该数字的位置。所以在上面的例子中,它将是位置2​​,并找到我们将使用 MATCH()并使用MATCH的位置,我们将需要 3

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

所以要从数组中检索 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)))

现在我们有了 3 ,所以我们将使用它匹配找到可能值中的头寸

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)

这将给我们 2

现在我们知道这个号码在可能值中的位置。要找到这个数字,我们将使用 INDEX

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)

样本工作簿

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

这篇关于对于一个范围内的每个单元格,如果在下一个单元格中找到一个单独的范围内的值,那么可以做到这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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