简体   繁体   English

转到范围并将具有特定值的单元格添加到新范围

[英]Go to Range and add cells with specific value to a new range

I'm bit new with vba in Excel. 我对Excel中的vba有点陌生。 I'm trying to make a funtion to check a range for a specific text and add the cell containing the value to a new range. 我正在尝试检查特定文本的范围,并将包含该值的单元格添加到新范围。 And return the new range. 并返回新范围。

I found a piece of code from brettdj on almost the same matter and alter it a bit for my situation. 我从brettdj找到了几乎相同的代码,并根据我的情况对其进行了一些更改。

The function look like: 该函数如下所示:

Function Test(Testvalue As String, TargetRange As Range) As Range

  Dim rng2 As Range
  Dim c As Range

  For Each c In TargetRange
    If c.Text = Testvalue Then
        If Not rng2 Is Nothing Then
        ' Add the 2nd, 3rd, 4th etc cell to our new range, rng2
        ' this is the most common outcome so place it first in the IF test (faster coding)
            Set rng2 = Union(rng2, c)
        Else
        ' the first valid cell becomes rng2
            Set rng2 = c
        End If
    End If
    Next
  Set Test = rng2
End Function

But when I put this in use in excel, for example in =IsBlank(test(Apple;A1:A5)) it returns a #VALUE!. 但是,当我在excel中使用它时,例如在= IsBlank(test(Apple; A1:A5))中,它返回一个#VALUE!。

Has somebody an idea how i can get this to work. 有一个想法我怎么能使它工作。 Many thz in advance 提前很多

Cell addresses are of the String type, not a Range type, so you can't return both from the function. 单元格地址是String类型,而不是Range类型,因此您不能从函数中返回两者。 User Defined Functions (UDFs) cannot return Range objects. 用户定义的函数(UDF)无法返回Range对象。 What you can do is return the addresses of each cell: 可以做的是返回每个单元格的地址:

Function Test(Testvalue As String, TargetRange As Range) As String
  Dim rng2 As String
  Dim c As Range

    For Each c In TargetRange
        If c.Text = Testvalue Then
            If rng2 <> vbNullString Then
                ' Add the 2nd, 3rd, 4th etc cell to our new range, rng2
                ' this is the most common outcome so place it first in the IF test (faster coding)
                rng2 = rng2 & "," & c.Address
            Else
                ' the first valid cell becomes rng2
                rng2 = c.Address
            End If
        End If
    Next
    Test = rng2
End Function

The output from this function is a comma-delimited list of cell addresses where the string was found. 此函数的输出是找到字符串的单元格地址的逗号分隔列表。 (B3 contains the formula, B2 shows what the formula in B3 looks like.) (B3包含公式,B2显示了B3中的公式。)

用法示例

To use this string of cell addresses, you'd have to create a different UDF (although a UDF cannot modify a different cell's contents or formatting): 要使用此单元格地址字符串,您必须创建另一个UDF(尽管UDF无法修改其他单元格的内容或格式):

Function test2(TestValue As String) As String
    Dim c As Range
    For Each c In Range(TestValue)
        MsgBox "The cell's address is: " & c.Address
    Next c
    test2 = "Last calculated on " & Now()
End Function

If you're trying to modify in any way the cells that contain the text "Apple", you should consider using a different approach. 如果您试图以任何方式修改包含文本“ Apple”的单元格,则应考虑使用其他方法。

additional variant to already provided 已提供的其他变体

Function Test(Testvalue As String, TargetRange As Range) As String
  Dim d As Object: Set d = CreateObject("Scripting.Dictionary")
  Dim c As Range
  For Each c In TargetRange
    If c.Value2 = Testvalue Then d.Add c.Address(0, 0), ""
  Next
  Test = Join(d.keys, ","):  Set d = Nothing
End Function

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

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