我在Excel中與vba有點新。 我試圖做一個函數來檢查特定文本的範圍,並將包含該值的單元格添加到新範圍。並返回新的範圍。轉到範圍,並將具有特定值的單元格添加到新範圍
我在brettdj上發現了幾乎相同的代碼,並對其進行了一些修改。
功能樣子:
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
但是,當我把這個用在Excel中,例如在= ISBLANK(測試(蘋果; A1:A5)),它返回一個#VALUE!
有人想法我怎麼能得到這個工作。 許多thz提前
你想要的功能,以什麼樣的回報?我測試了這個函數,它返回了'FALSE',而不是'#VALUE!'。你想讓該函數返回true/false,還是希望它返回找到值的單元格地址? – ARich
你好ARICH,我想返回單元格作爲範圍。所以我可以在其他函數中使用輸出,其中有一個範圍作爲輸入。在我的問題中有一個錯誤,我使用Countblank而不是IsBlank對其進行了測試。結果#Value! –