Solved

Move cells containing keywords to another column

Posted on 2011-02-18
5
768 Views
Last Modified: 2012-05-11
Column A has about 5K rows of domain names, Column B has about 20 keywords (each keyword in one row). How do I move the ones in A which contain keywords to Column C, and shift empty cells up in A?
0
Comment
Question by:mmcompact
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 34929944
Well, depending on how easily you want this done, you could always use a VLOOKUP to locate a keyword and, based on that, copy it into C.  however, if your goal is to get a new version of column A, it seems that a macro might be the way to go.

so starting with the VLOOKUP idea, in cell C1 put this:

=VLOOKUP(B1, $A$1:$A$9999, 1, FALSE)

If the keyword exists, you should see the domain name otherwise you'll see an error.
Copy this formula down next to each keyword and you'll see the first domain name that contains that keyword.
If you want to show blank instead of the error, try this:

=If(ISERR(VLOOKUP(B1, $A$1:$A$9999, 1, FALSE)), "", VLOOKUP(B1, $A$1:$A$9999, 1, FALSE))

Is that a good start or am I off track for what you want?
0
 

Author Comment

by:mmcompact
ID: 34929985
both Column A and keywords list are different each time when I get a new file. VB code will be better than VLookup
See the sample file, I need to move the ones in A to C, the actual file has 5000 rows


Book1.xls
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 34930181
The attached code seems to work for me.  I simply added a button and linked it to that code.

Sub GetKeyDoms_Click()
    Dim iRowKey As Integer
    Dim iRowDom As Integer
    Dim iDomCount As Integer
    Dim iRowMatches As Integer
    Dim strKeyword As String
    Dim strDomain As String
    
    Range("A2").End(xlDown).Select
    iDomCount = Selection.Row
    
    iRowMatches = 2 ' set the next row for a matched item
    iRowKey = 2 ' set the next row for a keyword to check
    Do
        ' get the next keyword
        strKeyword = Cells(iRowKey, 2).Value
        If strKeyword = "" Then
            Exit Do
        End If
        
        ' see if it matches any domains
        For iRowDom = iDomCount To 2 Step -1
            strDomain = Cells(iRowDom, 1).Value
            If strDomain = "" Then
                Exit Do
            End If
            
            If InStr(strDomain, strKeyword) > 0 Then
                ' keyword found, copy it
                Cells(iRowMatches, 3).Value = strDomain
                iRowMatches = iRowMatches + 1
                ' and delete the original
                Cells(iRowDom, 1).Delete Shift:=xlUp
                iDomCount = iDomCount - 1
            End If
        Next
        
        iRowKey = iRowKey + 1
    Loop
End Sub

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34930191
Oh, and at the end, you'll probably want to add something like:

Range("A2").Select

so the cursor ends up at a nice place instead of the original end of the domain list.
0
 

Author Closing Comment

by:mmcompact
ID: 34930293
thank you!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question