Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel - Update Code To Exclude Row If Certain Text Found

Posted on 2013-01-10
7
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 1000 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 49

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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.

722 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