VBA code to look at cell data and pick out a certain word?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have various data in column C on my sheet.

Here are a couple of examples:

NESC DLC GST Esp Honduras 12Cap 6x72g QW

NESCAFE DLC GST LtMac 16Cap 6x194.4g QW

Most of the data is different but the 12Cap & 16Cap are always there and it's the last entry in column C I am after.

I was wondering if it was possible a vba code could find these and returns a message box saying 12Cap and the same for 16Cap.

I hope this makes sense.

Thanks

Dan
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
If you want to check only the last data cell in column C and it will always contain either 12Cap or 16Cap then :
Code:
Dim c As Range: Set c = Cells(Rows.Count, "C").End(xlUp)
MsgBox Mid(c, InStr(c, "Cap") - 2, 5)
 
Upvote 0
Hi footoo,

Thank you for your reply.

I forgot to mention about the formula in the cell below where the data is. Sorry.

Is it possible you could alter your code to look at the second to last cell please?

When I deleted the formula below your code worked perfectly.

Thanks again

Dan
 
Upvote 0
Code:
Dim c As Range: Set c = Cells(Rows.Count, "C").End(xlUp)(0)
MsgBox Mid(c, InStr(c, "Cap") - 2, 5)
 
Upvote 0
Hi footoo,

I have just come across a problem with your code.

If I enter this market in :

[TABLE="width: 285"]
<tbody>[TR]
[TD="class: xl58890, width: 285"]Capp 16Cap 6x186.4g NI QW
[/TD]
[/TR]
</tbody>[/TABLE]
it comes up with this error 'Run time error 5. Invalid procedure call or argument'.

Is it because there is a Capp and Cap in the line and is there a way to solve it?

Thanks again for your help.

Regards

Dan
 
Upvote 0
try this :
Code:
Dim c As Range: Set c = Cells(Rows.Count, "C").End(xlUp)(0)
If InStr(c, " 12Cap ") <> 0 Then
    MsgBox Mid(c, InStr(c, " 12Cap ") + 1, 5)
Else
    MsgBox "16Cap"
End If
 
Upvote 0
Code:
Dim c As Range: Set c = Cells(Rows.Count, "C").End(xlUp)(0)
If InStr(c, " 12Cap ") <> 0 Then
    MsgBox "12Cap"
Else
    MsgBox "16Cap"
End If
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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