简体   繁体   中英

VBA look down column in Excel and find value?

I am trying to find a value by looking down column E in another workbook.

The value is hidden in amongst text in a cell.

Cell Example:

This Is Some Text My Value This is some text

Here's my code:

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

Nothing seems to happen, and excel crashes.

Please can someone show me where i am going wrong?

One of your issues is that your rng variable is referring to an entire column, and you are then asking the code to take the value.

After your With statement, try this code;

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

The purpose of the String1:= and String2:= is to specify that these are the conditions you are referring to as the first condition for InStr is Start:=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM