Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Excel - Update Code To Exclude Row If Certain Text Found

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
Escanaba
Asked:
Escanaba
2 Solutions
 
redmondbCommented:
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
 
krishnakrkcCommented:
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
 
EscanabaAuthor Commented:
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
Technology Partners: 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!

 
redmondbCommented:
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
 
krishnakrkcCommented:
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
 
Martin LissRetired ProgrammerCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now