2015-05-04 53 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

非常感謝你,這已經完成了我所要求的。祝你有美好的一天! –

相關問題