Solved

Excel - Update Code To Exclude Row If Certain Text Found

Posted on 2013-01-10
7
345 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 250 total points
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

13 Experts available now in Live!

Get 1:1 Help Now