简体   繁体   English

查找其他单元格的确切值-Excel-VBA

[英]Find the exact value of a different cell - Excel - VBA

I want to be able to compare 2 cells in VBA. 我希望能够比较VBA中的2个单元格。 The issue i'm having is that it's not making an exact match. 我遇到的问题是,它没有完全匹配。

Dim WeekNum2 As Integer
WeekNum2 = Cells(1, 12).Value
Range("F2:F60").Find(WeekNum2).Activate
U = ActiveCell.Row
Range(Cells(U, 7), Cells(U, 7)).Value = GreenCountP4
Range(Cells(U, 8), Cells(U, 8)).Value = YellowCountP4
Range(Cells(U, 9), Cells(U, 9)).Value = RedCountP4

Above is the section of code that isn't working ideally. 以上是无法正常运行的代码部分。 The value in Cells(1,12) is generated using the =WEEKNUM(K1) formula so depending on the date in K1 it returns a value from 1-52. Cells(1,12)的值是使用=WEEKNUM(K1)公式生成的,因此根据K1中的日期,它会返回1-52的值。 I want it to take this value and then find the equivalent value in the range F2:F60. 我希望它采用此值,然后在F2:F60范围内找到等效值。

In F2:F60 i have values going in order from "w46"-"w52" and then "w1"-"w52". F2:F60我的值从“ w46”-“ w52”开始依次为“ w1”-“ w52”。 The issue is that if the value in Cells(1, 12) is 5 for example, it will select the first row that has a 5 in it in the range (w50 in this case). 问题是,例如,如果Cells(1, 12) (1,12 Cells(1, 12)值为5,它将选择范围为5(在本例中为w50)的第一行。

Is it possible to compare just the numbers in the cell (so not include the "w" with it still being present). 是否可以仅比较单元格中的数字(因此不包括仍存在的“ w”)。 If not, how do i make it so it picks up the exact values (So if the value in Cells(1, 12) is 5, then it goes to right 5 instead of the first 5 in the range) 如果不是,我该如何做才能使它获得准确的值(因此,如果Cells(1, 12)是5,那么它将转到右边的5,而不是范围中的前5)

You can "cheat" a little, make the value of 5 as "W5" and then find a match for that in your range. 您可以“作弊”一点,将5的值设为“ W5”,然后在您的范围内找到与之匹配的内容。 Just add another variable, let's call it WeekNum2String and add the "W" as a prefix. 只需添加另一个变量,我们将其WeekNum2String并添加“ W”作为前缀。 Now you can search your range with Range("F2:F60").Find(WeekNum2String) . 现在,您可以使用Range("F2:F60").Find(WeekNum2String)搜索范围。

Dim WeekNum2String As String

WeekNum2String = "W" & Cells(1, 12).Value

Note : you should use the Find method by defining a Range, then setting it to the Find result (and not with Activate ) .This method will allow you to trap errors if there is no Find . 注意 :您应该使用Find方法来定义一个Range,然后将其设置为Find结果(而不是使用Activate )。如果没有Find方法将允许您捕获错误。

Like this: 像这样:

Dim FindRng As Range
Set FindRng = Range("F2:F60").Find(WeekNum2String)

If Not FindRng Is Nothing Then
    U = FindRng.Row
Else
    MsgBox "Week number " & Cells(1, 12).Value & " not found in Range"
End If

I suggest you use an optional argument to the find method, which allows you to insist that the entire cell is matched, and not just some subset of the text inside. 我建议您对find方法使用一个可选参数,该参数可让您坚持要匹配整个单元格,而不仅仅是内部文本的某些子集。 You want to use the LookAt argument, and set it to xlWhole. 您要使用LookAt参数,并将其设置为xlWhole。 Now nothing will be found for "5", because no cell will match "5" entirely. 现在将找不到“ 5”的任何内容,因为没有单元格将完全匹配“ 5”。 Cells containing "w5" aren't a match with LookAt:=xlWhole. 包含“ w5”的单元格与LookAt:= xlWhole不匹配。 I know, the naming of arguments is not at all intuitive... 我知道,参数的命名完全不直观。

Furthermore, life would be much easier if you simply used a string "w5" in your search. 此外,如果仅在搜索中使用字符串“ w5”,则生活会容易得多。 An alternative would be to strip the w's out of the cells in the find (search) range. 一种替代方法是将w移出查找(搜索)范围内的单元格。 This can be done with the split method. 这可以通过split方法完成。

use xlWhole value for lookat argument of Find() method: 使用xlWhole值作为Find()方法的lookat参数:

Range("F2:F60").Find("W" & WeekNum2, lookat:=xlWhole, LookIn:=xlValues).Activate
U = ActiveCell.Row

furthermore you can avoid Select and directly go like follows: 此外,您可以避免Select并直接执行以下操作:

U = Range("F2:F60").Find("W" & WeekNum2, lookat:=xlWhole, LookIn:=xlValues).Row

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

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