2016-08-24 102 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

相关问题