2017-06-02 104 views
0

我試圖找到另一個工作簿低頭E列的值。VBA在Excel中查看列並找到值?

值之間在單元格中的文字隱藏英寸

電池實施例:

這是一些文本My Value這是一些文本

這裏是我的代碼:

Option Explicit 
Sub Find() 
Dim FoundRange As Range 
Dim rng As Range 
Dim wb As Workbook 

On Error Resume Next 
Set wb = Workbooks("Supplier Contacts.xlsx") 
If wb Is Nothing Then 'open workbook if not open 
    Set wb = Workbooks.Open("G:\QUALITY ASSURANCE\06_SUPPLIER INFORMATION\Supplier Contacts.xlsx") 
End If 

With wb.Worksheets("Listed Supplier") 

Set rng = .Columns("E:E") 
Do While rng.Value <> Empty 
If InStr(rng.Value, ThisWorkbook.Worksheets("Data").Range("B3").Value) = 0 Then 
Set rng = rng.Offset(1) 
rng.Select 

Else 

MsgBox "I contain a % symbol!" 
End If 
Loop 

End With 
End Sub 

好像沒有什麼改變和Excel崩潰。

請能有人告訴我在哪裏,我錯了?

+0

不使用'Empty'使用'「」'你做。我相信你有你的InStr標準倒退。 –

+3

您可能想查看Find()和/或Application.WorksheetFunction.Match。它們都會比循環更快。 –

+0

@ScottCraner我是affraid我不知道如何做到這一點。我是VBA的全新人物。你能給我看看嗎? – user7415328

回答

0

你的一個問題是,你rng變量是指整列,然後你問的代碼,以價值。

With語句後,試試這個代碼;

For Each rng in wb.Range("E:E") 
    If IsEmpty(rng.value) = False Then 
     If InStr(String1:=rng.Value, String2:=ThisWorkbook.Worksheets("Data").Range("B3").Value) <> 0 Then 
     MsgBox "I contain a % symbol!" 
     End If 
    End If 
Next rng 

String1:=String2:=的目的是爲了說明,這些是你指的是爲InStr的首要條件是條件Start:=