Solved

Excel - Update Code To Exclude Row If Certain Text Found

Posted on 2013-01-10
7
379 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
Industry Leaders: 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!

 
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 47

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

685 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