Solved

Move cells containing keywords to another column

Posted on 2011-02-18
5
770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

726 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