Vba - count string within string

beruf3

New Member
Joined
Nov 20, 2015
Messages
43
I have product codes: (they are in C column of active sheet)
DO-001
DO-002
DO-003
DO-004

And I have big list of data: (they are in C column of "Sheet1")
41300100_DO-001_14215171
41300104_DO-001_14215173
K1_ISK_41300661_DO-002_13190369
NP_41533258_DO-003_14910884
DO-003_DD_44_ddd

And I want to count how many times do the product codes appear in the list of data.

So the result for this case'd be: (result is H column of active sheet)
DO-001 2
DO-002 1
DO-003 2
DO-004

I have done this with this code:

Sub CountcodesPLC()
Dim i, j As Integer, icount As Integer
Dim ldata, lcodes As Long

icount = 0

lcodes = Cells(Rows.Count, 3).End(xlUp).Row
ldata = Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row

For i = 10 To lcodes
For j = 2 To ldata
If InStr(Worksheets("Sheet1").Range("C" & j), Range("C" & i)) <> 0 Then
icount = icount + 1
End If
Next j
If icount <> 0 Then
Range("H" & i).Value = icount
End If
icount = 0
Next i
End Sub

But I want to change so if the list of data contains some key words like "NP", "ISK",
then not to count them, or if the first part of the data is the code then also not to count them,
so the result would be:

DO-001 2
DO-002
DO-003
DO-004

Also, I'll have around 1.000 product codes, and around 60.000 strings of data.
Will my code crash?
 
Also,because I have like 8 different product codes "DO-", "CI-", "CA-",...
Can I change the part of code

Code:
[COLOR=#333333]Left([/COLOR]<acronym title="vBulletin" style="color: rgb(51, 51, 51); font-size: 12px; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help;">vb</acronym>[COLOR=#333333](i, 1), 3) = "DO-" Then[/COLOR]


to

Code:
[COLOR=#333333]Left([/COLOR]<acronym title="vBulletin" style="color: rgb(51, 51, 51); font-size: 12px; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help;">vb</acronym>[COLOR=#333333](i, 1), 3) = Left(va(i,1),3) Then[/COLOR]

That's not gonna work. You need to include each item to the code, like this:
Code:
[COLOR=#0000ff]If InStr(vb(i, 1), "NP") Or InStr(vb(i, 1), "ISK") Or Left(vb(i, 1), 3) = "DO-" Or Left(vb(i, 1), 3) = "CI-" Or Left(vb(i, 1), 3) = "CA-"  ' [/COLOR][COLOR=#000000].. and so on [/COLOR]





And I want, if the count is zero, to left it blank ("").
Is that like this:

Code:
If vd > 0
Range("H2").Resize(UBound(vd, 1), 1) = vd

Just delete the red line:
Code:
For i = 1 To UBound(va, 1)
[COLOR=#ff0000]vd(i, 1) = 0[/COLOR]
    For Each x In d
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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