vba - find last instance of a text string

CROY1985

Well-known Member
Joined
Sep 21, 2009
Messages
501
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have Some data which looks like this:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Row[/TD]
[TD]Data[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Onions[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Onions[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Onions[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Potatoes[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Potatoes[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Potatoes[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Spring Onions[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Spring Onions[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Spring Onions[/TD]
[/TR]
</tbody>[/TABLE]


Im using the following vba code which isnt quite getting the right answer:

Code:
    Set where = C.Find(what:="Onions", after:=C(1), searchdirection:=xlPrevious)
    endrow = where.Row

But the vba code is finding the Onions in "Spring Onions" and returns row 9, i need it to match contents exactly and return row 3 in the example above.

Can anyone help on adjusting this?


Thank you
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try
Code:
Set Where = c.find("Onions", c(1), , xlWhole, , xlPrevious, False, , False)
 
Upvote 0
Try:
Code:
Dim endrow As Long
endrow = Range("C:C").Find("Onions", LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious).Row
Change "C:C" to suit your needs.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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