How to Creating a Unique Keyword List?

John Caines

Well-known Member
Joined
Aug 28, 2006
Messages
1,155
Office Version
  1. 2019
Platform
  1. Windows
Dear Forum members,

Can anyone help me?
What I'm trying to achieve is the following;
I have a big keyword list saved in Excel.
Something like the following,
These are all keyword phrases;

car rent
car hire
cars for rent uk
etc etc etc

All listed in Column A

All phrases in Column A, and in separate rows.(1 phrase per row.)about 2000 lines (Rows) in total.
What I want to know is there any way of selecting the whole list and exporting it (To save it as another list. A list of just unique keywords??

So, It would create a list like;
car
rent
uk
hire
for
etc etc etc.

So basically I want excell to look at all the words and export them to another list showing just unique keywords, 1 per line.

This is so I can see from a huge list what all the unique keywords are.
Is there a way of doing this within Excel Now?? or has someone made a plugin ( Macro) to achieve this??
I've looked at the sort & filter options, but it doesn't appear to have this function?

Any thoughts on this would be great.
I've wanted to do this for ages and I'm a basic beginner in excel and just can't work it out.:(

Hope someone can help.
Many Thanks
John
Many Thanks
John
 
Jindon - Over My head

Hello Jindon,
Apologies first.
I thought your formula was trying to do the same as Krishna's.
I can see why your's has more lines of code in it now:) It went over my head totally:)
Actually, though Jindon, the error is still there. It won't work.
I can attach jpegs of the errors if you pm me your email if you do need them, and my workbook if you like

But the errors are as follows .
1. I go to visual basic and enter your code.
2. then there are several ways I've tried it, all don't work. I either
have my mouse over the VB window with your code in and press F5, or I go "File"/ "Close and return to Excel" the go to "Tools"/MACRO/RUN MACRO,,,, Or I've also tried in VB going up to "Run" /runsub/userform/

All return this error message.
"Compile error:
Duplicate declaration in current scope.

If I click ok, the first line of "Sub Test()" is in yellow colour with an arrow pointing to it and the , b() on line 3 is in grey??????

Hope this all makes sense Jindon
The code looks awesome from a point of that I haven't got a clue what it means, but it looks impressive anyway:)
Hope this makes sense
John
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
John,

Can you just delete one "b()" in the Dim statement?

Previous code has been edited:
 
Upvote 0
Jindon

Hello Jindon ,
Thanks for your reply.

You said"Can you just delete one "b()" in the Dim statement? "

I'm a newbie Jindon, you know, I have to look up in help to find out why my title columns are frozen and how to unfreeze them:)

Anyway, from what you said, I looked at your formula and tried find a(b)which I would just remove.
In notepad++ this was on line No. 2,the line was below;
--------------------------------------------------------------------------
Dim ws As Worksheet, a, e, dic As Object, b(), m As Object
---------------------------------------------------------------------------
I removed the b()
Hope this is what you meant??
So the line now looks like;
---------------------------------------------------------------
Dim ws As Worksheet, a, e, dic As Object, M As Object
--------------------------------------------------------------
I've then tried to run it again, but get an error and this line is highlighted in yellow,
-------------------------------------------------------------
myTxt = Applilcation.InputBox("Enter a word", Type:=2)
---------------------------------------------------------------
This is on line 10 in notepad,
I thought and tried changing the "Enter Word" to a word for what I want to search for, to see if that was the problem, but that still did nothing.
Seems the error is here Jindon if I have removed the (b) from line 2 ok??

I then ran the macro again, same. "run time error '424' object required
Still the line above (Line 10 of notepad++) is highlighted in yellow.

Sorry Jindon,
Maybe I haven't deleted the b correctly???
Not sure.
Thanks for trying anyway.
I feel abit guilty, as I don't want you to waste your time on this, I expect your busy. Also a guy called "Krish" posted something earlier that worded and done the job.
I'd have liked to have seen how yours did run though.
Again. Thanks for trying Jindon
All the best
John
 
Upvote 0
Sorry another typo. Applilcation -> Application

Can you just delete whole old code and replace with the folloiwng code?
Code:
Sub test()
Dim ws As Worksheet, a, e, dic As Object, b(), m As Object
Dim n As Long, myTxt As String, txt As String
Set dic = CreateObject("Scripting.dictionary")
dic.CompareMode = vbTextCompare
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Reuslt"
On Error GoTo 0
myTxt = Application.InputBox("Enter a word",Type:=2)
ReDim b(1 To Rows.Count, 1 To 2)
With CreateObject("VBScript.RegExp")
    For Each ws In Sheets
        If ws.Name <> "Result" Then
            a = ws.Range("a1",ws.Range("a" & Rows.Count).End(xlUp)).Value
            If IsArray(a) Then
            For Each e In a
                If InStr(1,e,myTxt,1) > 0 Then
                     txt = Replace(e, MyTxt,"")
                    .Pattern = "[,\?\*\.\|\{\}\\\[\]\(\)!]"
                    .Global = True
                    txt = .replace(txt,"")
                    .Pattern = "\S+"
                    .Global = True
                    For Each m In .execute(txt)
                        If Not dic.exists(m.Value) Then
                            n = n + 1
                            b(n,1) = m.Value : b(n,2) = 1
                            dic.add m.Value, n
                        Else
                            b(dic(m.Value),2) = b(dic(m.Value),2) + 1
                        End If
                    Next
                End If
            Next
            Else
                If InStr(1,ws.Range("a1").Value, mtTxt,1) > 0 Then
                    txt = Replace(ws.Range("a1").Value, myTxt,"")
                    .Pattern = "[,\?\.\*\|\{\}\[\]\(\)!]"
                    .Global = True
                    txt = .replace(txt, "")
                    .Pattern = "\S+"
                    .Global = True
                    For Each m In .execute(txt)
                        If Not dic.exists(m.Value) Then
                            n = n + 1
                            b(n,1) = m.Value : b(n,2) = 1
                            dic.add m.Value, n
                        Else
                            b(dic(m.Value), 2) = b(dic(m.Value),2) + 1
                        End If
                    Next
                End If
            End If
        End If
    Next
End With
Set dic = Nothing : Erase a
With Sheets("Result").Range("a1")
    .Resize(,2).Value = [{"Word","# of appearance"}]
    .Offset(1).Resize(n,2).Value = b
    .CurrentRegion.Sort key1:=.Range("b1"), order1:=xlDescending, header:=xlYes
End With
End Sub
 
Upvote 0
Sorry Jindon

Line 59 - error:(

VSortMD b, 1, n, 2

It says, sub or function nor defined????

Still no joy:(
Sorry Jindon.
Almost there I think:)
 
Upvote 0
Sorry Can you just delete that line please?

And one more

header:=xlNo should be

header:=xlYes

previous code has benn edited already
 
Upvote 0
no joy again:(

Sorry Jindon.
I just copied your code that you said you'd alterd which was;
----------------------------------------------------------------------------
Sub test()
Dim ws As Worksheet, a, e, dic As Object, b(), m As Object
Dim n As Long, myTxt As String, txt As String
Set dic = CreateObject("Scripting.dictionary")
dic.CompareMode = vbTextCompare
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Reuslt"
On Error GoTo 0
myTxt = Application.InputBox("Enter a word",Type:=2)
ReDim b(1 To Rows.Count, 1 To 2)
With CreateObject("VBScript.RegExp")
For Each ws In Sheets
If ws.Name <> "Result" Then
a = ws.Range("a1",ws.Range("a" & Rows.Count).End(xlUp)).Value
If IsArray(a) Then
For Each e In a
If InStr(1,e,myTxt,1) > 0 Then
txt = Replace(e, MyTxt,"")
.Pattern = "[,\?\*\.\|\{\}\\\[\]\(\)!]"
.Global = True
txt = .replace(txt,"")
.Pattern = "\S+"
.Global = True
For Each m In .execute(txt)
If Not dic.exists(m.Value) Then
n = n + 1
b(n,1) = m.Value : b(n,2) = 1
dic.add m.Value, n
Else
b(dic(m.Value),2) = b(dic(m.Value),2) + 1
End If
Next
End If
Next
Else
If InStr(1,ws.Range("a1").Value, mtTxt,1) > 0 Then
txt = Replace(ws.Range("a1").Value, myTxt,"")
.Pattern = "[,\?\.\*\|\{\}\[\]\(\)!]"
.Global = True
txt = .replace(txt, "")
.Pattern = "\S+"
.Global = True
For Each m In .execute(txt)
If Not dic.exists(m.Value) Then
n = n + 1
b(n,1) = m.Value : b(n,2) = 1
dic.add m.Value, n
Else
b(dic(m.Value), 2) = b(dic(m.Value),2) + 1
End If
Next
End If
End If
End If
Next
End With
Set dic = Nothing : Erase a
With Sheets("Result").Range("a1")
.Resize(,2).Value = [{"Word","# of appearance"}]
.Offset(1).Resize(n,2).Value = b
.CurrentRegion.Sort key1:=.Range("b1"), order1:=xlDescending, header:=xlYes
End With
End Sub
-----------------------------------------------------------------------------------
Had a code 9 error, "Subscript out of range"
this is highlighted in yellow now;
----------------------------------------------
With Sheets("Result").Range("a1")
---------------------------------------------
But it did get to the stage of me entering a name in a pop up box:)
Almost there

It's all goobledegoop to me:)
 
Upvote 0
OK good sign
try
Code:
Sub test()
Dim ws As Worksheet, a, e, dic As Object, b(), m As Object
Dim n As Long, myTxt As String, txt As String
Set dic = CreateObject("Scripting.dictionary")
dic.CompareMode = vbTextCompare
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Reuslt"
On Error GoTo 0
myTxt = Application.InputBox("Enter a word",Type:=2)
ReDim b(1 To Rows.Count, 1 To 2)
With CreateObject("VBScript.RegExp")
    For Each ws In Sheets
        If ws.Name <> "Result" Then
            a = ws.Range("a1",ws.Range("a" & Rows.Count).End(xlUp)).Value
            If IsArray(a) Then
            For Each e In a
                If InStr(1,e,myTxt,1) > 0 Then
                     txt = Replace(e, MyTxt,"")
                    .Pattern = "[,\?\*\.\|\{\}\\\[\]\(\)!]"
                    .Global = True
                    txt = .replace(txt,"")
                    .Pattern = "\S+"
                    .Global = True
                    For Each m In .execute(txt)
                        If Not dic.exists(m.Value) Then
                            n = n + 1
                            b(n,1) = m.Value : b(n,2) = 1
                            dic.add m.Value, n
                        Else
                            b(dic(m.Value),2) = b(dic(m.Value),2) + 1
                        End If
                    Next
                End If
            Next
            Else
                If InStr(1,ws.Range("a1").Value, mtTxt,1) > 0 Then
                    txt = Replace(ws.Range("a1").Value, myTxt,"")
                    .Pattern = "[,\?\.\*\|\{\}\[\]\(\)!]"
                    .Global = True
                    txt = .replace(txt, "")
                    .Pattern = "\S+"
                    .Global = True
                    For Each m In .execute(txt)
                        If Not dic.exists(m.Value) Then
                            n = n + 1
                            b(n,1) = m.Value : b(n,2) = 1
                            dic.add m.Value, n
                        Else
                            b(dic(m.Value), 2) = b(dic(m.Value),2) + 1
                        End If
                    Next
                End If
            End If
        End If
    Next
End With
Set dic = Nothing : Erase a
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Result"
On Error GoTo 0
With Sheets("Result").Range("a1")
    .Resize(,2).Value = [{"Word","# of appearance"}]
    .Offset(1).Resize(n,2).Value = b
    .CurrentRegion.Sort key1:=.Range("b1"), order1:=xlDescending, header:=xlYes
End With
End Sub
 
Upvote 0
Still no joy:(

Hi Jindon,

still get an error on a line of code here;
The pop up box to enter a phrase comes up fine, I type in a phrase then I get a "Run-Time error"13" Type mismatch appear?????????????
I click debug and it goes to VB.
--------------------------------------------------------
Set dic = Nothing: Erase a
-----------------------------------------
The " :Erase a" is highlighted in yellow.:(

Any clues????
All beyond me I'm afraid:(

All the best
John
 
Upvote 0
slight tweek to macro if Possible???

Hello everyone.

Many thanks to Krish and Jindon for your help so far.

I'll try and explain in this post where I am now with a macro some of you have all helped me with, which I'm very grateful for, and what I'd like it to be able to do. (Just tweek it slightly).

OK. just to explain what I'm trying to do,
=========================
I have a big keyword phrase list.
maybe 10,000 lines of phrases.

I would like a button that on a new sheet creates the following;
The button would pop up and in it you would type a word or phrase. It then on a new sheet returns all the lines of phrases found from the main keyword list that contain the said word or phrase on separate rows in the new sheet.
So;
3 separate columns ;
Column 1 on the sheet would be called “Keyword Phrases” this is the heading and would be in cell A1 .
Second column would be called “Unique Keywords” in cell C1.

And the new third column I would like to have added would be called “ No Unique Keywords” in cell E1. (This would return the number of times the unique keyword has been found within the returned list, so it kind of gives the unique keywords a weighting of popularity, as the higher the number, so the more the unique keyword has been found, and as such is more popular.

All the results would be returned from row 3 onwards (So there are the headings, miss a row, then the results).

So far Krish who has been very helpful has written this macro which does 2 of the 3 columns;
========================================

Option Explicit

Sub TestIt_v01()

Dim sWS As Worksheet, _

rWS As Worksheet, _

FindStr As String

Dim n As Long, _

k As Long

Dim a, v, x, w(), j, i

Application.ScreenUpdating = False

Set sWS = Sheets("Main KW List") 'change to suit

FindStr = Application.InputBox("Siam Sites Niche Keyword Finder", "Keywords To Find")

If FindStr = "" Then Exit Sub

If Application.WorksheetFunction.CountIf(sWS.Columns(1), "*" & FindStr & "*") = 0 Then Exit Sub

With CreateObject("Scripting.dictionary")

With sWS.Range("A1:A" & sWS.Range("A" & Rows.Count).End(xlUp).Row)

a = .Value

End With

For Each v In a

If Not IsEmpty(v) And InStr(1, v, FindStr) > 0 And _

Not .exists(v) Then

.Add v, Nothing

End If

Next

x = .keys

End With

On Error Resume Next

Application.DisplayAlerts = False

Sheets("Niche KW Result").Delete

Set rWS = Sheets.Add

rWS.Name = "Niche KW Results"

Application.DisplayAlerts = True

On Error GoTo 0

With rWS.Range("A1:A" & UBound(x) + 1)

.Resize(UBound(x) + 1, 1) = Application.Transpose(x)

.HorizontalAlignment = xlGeneral

.Columns.AutoFit

End With

With CreateObject("Scripting.dictionary")

For Each v In x

v = Trim(Replace(v, FindStr, ""))

i = Split(v, " ")

For j = 0 To UBound(i)

If Not IsEmpty(i(j)) And Not .exists(i(j)) And Not IsNumeric(i(j)) Then

k = k + 1: ReDim w(1 To k, 1 To 1)

w(k, 1) = i(j): .Add w(k, 1), Nothing

End If

Next j

Next v

rWS.Range("C1") = "Unique Keywords"

rWS.Range("C2").Resize(k, 1) = Application.Transpose(.keys)

rWS.Range("C2").Resize(k + 1, 1).Sort Key1:=rWS.Range("C2"), Order1:=xlAscending, Header:=xlGuess, _

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

rWS.Range("C1").Resize(k + 1, 1).HorizontalAlignment = xlGeneral

rWS.Columns(3).AutoFit

End With

Application.ScreenUpdating = True

End Sub
=======================================
Also, Jindon has written a formula, but unfortunately I stll can't get it to work yet, still get an error message, but I think it's written more for the third column of what I'm trying to do; here it is anyway;
======================================
Sub test()
Dim ws As Worksheet, a, e, dic As Object, b(), m As Object
Dim n As Long, myTxt As String, txt As String
Set dic = CreateObject("Scripting.dictionary")
dic.CompareMode = vbTextCompare
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Reuslt"
On Error GoTo 0
myTxt = Application.InputBox("Enter a word",Type:=2)
ReDim b(1 To Rows.Count, 1 To 2)
With CreateObject("VBScript.RegExp")
For Each ws In Sheets
If ws.Name <> "Result" Then
a = ws.Range("a1",ws.Range("a" & Rows.Count).End(xlUp)).Value
If IsArray(a) Then
For Each e In a
If InStr(1,e,myTxt,1) > 0 Then
txt = Replace(e, MyTxt,"")
.Pattern = "[,\?\*\.\|\{\}\\\[\]\(\)!]"
.Global = True
txt = .replace(txt,"")
.Pattern = "\S+"
.Global = True
For Each m In .execute(txt)
If Not dic.exists(m.Value) Then
n = n + 1
b(n,1) = m.Value : b(n,2) = 1
dic.add m.Value, n
Else
b(dic(m.Value),2) = b(dic(m.Value),2) + 1
End If
Next
End If
Next
Else
If InStr(1,ws.Range("a1").Value, mtTxt,1) > 0 Then
txt = Replace(ws.Range("a1").Value, myTxt,"")
.Pattern = "[,\?\.\*\|\{\}\[\]\(\)!]"
.Global = True
txt = .replace(txt, "")
.Pattern = "\S+"
.Global = True
For Each m In .execute(txt)
If Not dic.exists(m.Value) Then
n = n + 1
b(n,1) = m.Value : b(n,2) = 1
dic.add m.Value, n
Else
b(dic(m.Value), 2) = b(dic(m.Value),2) + 1
End If
Next
End If
End If
End If
Next
End With
Set dic = Nothing : Erase a
On Error Resume Next
Sheets("Result").Delete
Sheets.Add.Name = "Result"
On Error GoTo 0
With Sheets("Result").Range("a1")
.Resize(,2).Value = [{"Word","# of appearance"}]
.Offset(1).Resize(n,2).Value = b
.CurrentRegion.Sort key1:=.Range("b1"), order1:=xlDescending, header:=xlYes
End With
End Sub
=====================================

So, if anyone can kind of combine the 2 formulas really in some way so that on the new sheet it finds as stated above. (I'll just reiterate it again;)

3 separate columns ;
Column 1 on the sheet would be called “Keyword Phrases” this is the heading and would be in cell A1 .
Second column would be called “Unique Keywords” in cell C1.

And the new third column I would like to have added would be called “ No Unique Keywords” in cell E1.

All the results would be returned from row 3 onwards (So there are the headings, miss a row, then the results).

I really Hope someone can help me on this.
Everyone has been so helpful so far.
Hope it is possible to do this.

Many thanks Everyone

John Caines
 
Upvote 0

Forum statistics

Threads
1,217,366
Messages
6,136,128
Members
449,993
Latest member
Sphere2215

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