Solved

Excel - Update Code To Exclude Row If Certain Text Found

Posted on 2013-01-10
7
368 Views
Last Modified: 2013-02-12
Hello,

Im hoping someone can assist.  The following code looks at each row in a spreadsheet and copys a string of text that appears between the word Context_ and an @ symbol, then pastes that text string in a 2nd worksheet.  The code works great but I now need it modified so that the code looks at each row and if the text: BROWSER is found then the code skips that row (no copy or paste) and moves on to the next.

Any assistance would be greatly appreciated!

Sub DoIt()
   
    Dim LastR As Long
   
    With Worksheets("Sheet1")
        LastR = .Cells(.Rows.Count, "ag").End(xlUp).Row
    End With
   
    With Worksheets("Sheet2").Range("a2:a" & LastR)
        .EntireColumn.ClearContents
        .Formula = "=IF(Sheet1!AG2<>"""",MID(Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2)+8,SEARCH(""@"",Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2))-SEARCH(""Context "",Sheet1!AG2)-8),"""")"
        .Value = .Value
    End With
   
    MsgBox "Done"
   
End Sub
0
Comment
Question by:Escanaba
7 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38765419
Esscanaba,

A couple of queries, please...
(1) Must "BROWSER" be a whole cell or may it just be part of a cell? If the latter, how do you feel about a macro solution?
(2) Which version of Excel are you using?

Thanks,
Brian.
0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 250 total points
ID: 38766372
Your formula would be

.Formula = "=IF(ISNUMBER(SEARCH(""BROWSER"",Sheet1!AG2)),"""",IF(isnumber(search(""@"",Sheet1!AG2)),MID(Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2)+8,SEARCH(""@"",Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2))-SEARCH(""Context "",Sheet1!AG2)-8),""""))"

Kris
0
 
LVL 1

Author Comment

by:Escanaba
ID: 38784572
Brian - Im using Excel 2007.  The text Browser is part of the cell.  Its placed in a long text string populated out of a secondary data system.

Krish - Can you put that into a macro instead of a formula?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 250 total points
ID: 38784807
Hi, Escanaba.

Please see attached. I've removed the formula - at a possible cost in performance, but the benefits are that there are no blank rows and it's more flexible for future changes. (Roughly how many rows do you expect to have?)

The code is...
Option Explicit

Sub Copy_Rows()
Dim i          As Long
Dim j          As Long
Dim xStart     As Long
Dim xEnd       As Long
Dim xLast_Row  As Long
Dim xLast_Row2 As Long
Dim xHold      As String
Dim xShape     As Shape

Sheets("Sheet1").Activate

If ActiveSheet.UsedRange.Rows.Count > 0 Then xLast_Row = [A1].SpecialCells(xlLastCell).Row
If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

xLast_Row_2 = Sheets("Sheet2").Range("A1").SpecialCells(xlLastCell).Row
Sheets("Sheet2").Range("A1:A" & xLast_Row_2).EntireRow.Delete

j = 1
Range("A1").EntireRow.Copy Destination:=Sheets("Sheet2").Range("A1")

Application.ScreenUpdating = False
    
    For i = 2 To xLast_Row
        If Range("A" & i).EntireRow.Find(What:="BROWSER", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True) Is Nothing Then
            xHold = Range("A" & i)
            xStart = InStr(1, xHold, "Context ")
            xEnd = InStr(1, xHold, "@")
            If xStart <> 0 And xEnd <> 0 And xEnd > (xStart + 8) Then
                j = j + 1
                Worksheets("Sheet2").Range("a" & j) = Mid(xHold, xStart + 8, xEnd - xStart - 8) & " - " & i
            End If
        End If
    Next

For Each xShape In Sheets("Sheet2").Shapes
    If Mid(xShape.Name, 1, 7) = "TextBox" Then xShape.Cut
Next


Application.ScreenUpdating = True

MsgBox "Done - " & j - 1 & " entries copied."

End Sub

Open in new window

Regards,
Brian.
Copy-Rows.xls
0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 38785453
Hi,

When I said formula, it is the formula within the macro.

Sub DoIt()
    
    Dim LastR As Long
    
    With Worksheets("Sheet1")
        LastR = .Cells(.Rows.Count, "ag").End(xlUp).Row
    End With
    
    With Worksheets("Sheet2").Range("a2:a" & LastR)
        .EntireColumn.ClearContents
        .Formula = "=IF(ISNUMBER(SEARCH(""BROWSER"",Sheet1!AG2)),"""",IF(isnumber(search(""@"",Sheet1!AG2)),MID(Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2)+8,SEARCH(""@"",Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2))-SEARCH(""Context "",Sheet1!AG2)-8),""""))"
        .Value = .Value
    End With
    
    MsgBox "Done"
    
End Sub

Open in new window


Kris
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 38879534
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

786 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