Solved

Move cells containing keywords to another column

Posted on 2011-02-18
5
767 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

930 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now