2015-05-04 57 views
0
Sub test1() 

Dim Str As String 
Dim Search As String 
Dim Status As String 
Str = Cells(2, 5).Value 
Search = FDSA!Cells(2, 5).Value 
Status = FDSA!Cells(2, 10).Value 

    If InStr(Search, Str) = True Then 
       Status = "ok" 
    Else 
     End If 

End Sub 

我将用循环来构建它。我想检查一下Cells(2,5)中是否包含在FDSA!Cells(2,5)中。如果这是真的,那么我想标记FDSA!Cells(2,10)。我收到一个对象所需的消息。这是我看过示例和教程后可以想到的。让我知道你是否有问题比较不同的工作表中的两个单元格,设置不同的单元格为好

只有第二次在VBA工作。 提前致谢,Alexis M.

回答

0

您的语法参考工作表是不正确的。这可能会引发错误。您需要拨打Worksheets("FDSA"),而不是像使用FDSA!那样拨打电话。

另外,您必须将单元格值设置为等于Status才能生效。只需更改Status将不会将其写回工作簿。

另外InStr返回匹配的位置。如果你想知道是否有匹配,你需要检查回报是>0。此代码应该运行,并且希望比您当前的代码更接近正确。

Sub test1() 

Dim Str As String 
Dim Search As String 

Str = Cells(2, 5).Value 
Search = Worksheets("FDSA").Cells(2, 5).Value 


    If InStr(Search, Str) > 0 Then 
     Worksheets("FDSA").Cells(2, 10).Value = "ok" 
    End If 

End Sub 
+0

非常感谢你,这已经完成了我所要求的。祝你有美好的一天! –

相关问题