2016-08-24 100 views
0

我試圖檢查,如果某一範圍的內容是真的,它會執行一個功能。如果功能的單元格內容

With Sheets(1).[A1:A95] 
     If .Cells.Value = "text" Then 
      'Perform function 
     End If 
    End With 

但是我得到一個類型不匹配錯誤。請幫助。

+0

你檢查,如果在該範圍內的任何單元有搜索詞'「文本」'? –

+0

@SiddharthRout:我有7個文本需要在特定範圍內檢查,每個文本都有一個特定的函數來調用。 – Marco

+0

查看我發佈的答案 –

回答

2

如果您試圖檢查範圍內的每個單元格,然後嘗試使用這種方法。

Dim cCell As Range 

For Each cCell in Sheets(1).Range("$A$1:$A$95") 
    'To test to ensure cCell.Value is what you expect(can remove once working) 
    Debug.Print cCell.Value 
    If cCell.Value ="whateveryouwanttotestfor" Then 
     'Call your function here 
     Call myFunction 
    End If 

Next cCell 

爲了測試CCELL的多個值使用選擇案例

For Each cCell in Sheets(1).Range("$A$1:$A$95") 
    Select Case cCell.Value 
     Case "text1" 
      Call text1Function 
     Case "text2" 
      Call text2Function 
     'Do the rest that you need 
    End Select 
Next cCell 
+0

也可以通過'For Each cCell in Sheets(1)。[A1:A95]'。以防萬一OP是這樣的:P –

+0

正確,只是在不確定海報的先進程度時,更喜歡顯示標準方式。 – dinotom

+0

@dinotom這項工作謝謝,但我有另一個問題。 if函數似乎不起作用。不管文本是否爲假,它仍然執行該功能,並且我最多有7個文本需要使用7種不同的功能進行檢查。 – Marco

相關問題