?
Solved

Move cells containing keywords to another column

Posted on 2011-02-18
5
Medium Priority
?
775 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

864 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