instr issue with matching string in another string

ExcelEveryday1

New Member
Joined
Jan 7, 2016
Messages
20
Hi guys,

Instr doesnt seem to be matching properly for me, and I dont know what Im doing wrong. I get a match if my criteria value is the same as my search location value, but if the string is in the other string without matching it exactly, my code doesnt pick up on it.

----------------------------------------
dim cell as range
dim range1 as range
dim ws as worksheet

For Each cell In range1


If InStr(ws.Cells(10036, 4).Value, cell.Value) <> 0 Then
ws.Cells(10036, 2).Value = "freedom"
Else
End If
Next cell
-------------------------------------------

As an example of what I'm looking for:

(ws.Cells(10036, 4) = 2ABC_123
cell.value = "2ABC"

I also tried adding in an & "*" to my cell.value, but that didnt seem to help :(


Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Wait. If activesheet is not ws3, then my set ranges give me this error "Method range of object" worksheet failed. I could solve this by ws3.activate, but I would like know why this is happening?!
 
Upvote 0
This is your Set statement?

dim ws3 as worksheet
set ws3 as thisworkbook.sheets(3)

The second line should be:

Set ws3 = Thisworkbook.Sheets(3)

Also, are you sure that Sheets(3) is the one you want? If you've renamed or rearranged sheets, it might be better to use a name:

Set ws3 = Thisworkbook.Sheets("Sheet3")
 
Upvote 0
This is your Set statement?

dim ws3 as worksheet
set ws3 as thisworkbook.sheets(3)

The second line should be:

Set ws3 = Thisworkbook.Sheets(3)

Also, are you sure that Sheets(3) is the one you want? If you've renamed or rearranged sheets, it might be better to use a name:

Set ws3 = Thisworkbook.Sheets("Sheet3")

Good Point. I actually I have the sheet I want in in sheets("") format, I just changed it due to privacy concerns and it was the first thing that came to mind.

so in my statements I have

public range1 as range
------
dim ws3 as worksheet
Set ws3 = ThisWorkbook.Sheets("3")

Set Range1 = ws3.range(Cells(1, 5), Cells(2, 6))

------------

This format gives me the error mentioned above if I dont have ws3 as activesheet.
If I ws3.activate before setting my ws3.regions then it works.
 
Upvote 0
The Cells property needs to be qualified just like the Range property:

Set Range1 = ws3.Range(ws3.Cells(1, 5), ws3.Cells(2, 6))
 
Upvote 0
Andrew makes an excellent point. I still miss that sometimes. If you want, you can use a With statement:
Code:
With ws3
    Set Range1 = .Range(.Cells(1,5), .Cells(2,6))
End With
Notice the . before Range and Cells - this is an indication to VBA to use the object defined on the With statement. This is equivalent to what Andrew wrote. It's often a matter of personal style, although depending on the situation, one or the other can be much shorter than the other.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top