Solved

Go to Specific Cell and Delete all rows after.

Posted on 2011-09-05
8
355 Views
Last Modified: 2012-08-13
For every worksheet in the workbook, I need code to go to a  specific row where Cell A1 contains the value = " Change in File and delete that row and every row after.
0
Comment
Question by:mato01
  • 5
  • 3
8 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 36486591
sure.  Put this code in a public module and run it.  It looks for that exact match.

 
Sub checkA1toClear()
Dim ws As Worksheet
Dim wk As Workbook

    Const strMatch = " Change in File and delete that row and every row after."

    Set wk = ThisWorkbook 'or could be activeworkbook if your running against another
    
    
    For Each ws In wk.Worksheets
        If ws.Range("A1").Value = strMatch Then
            ws.Cells.Clear 'will delete row A1 and all rows after = frankly, the entire sheet
        End If
    Next ws
End Sub

Open in new window


if you're looking for cell A1 to start with that text, but could have other text, just change line 11 to:

        If ws.Range("A1").Value Like strMatch & "*" Then 'looks for that string and anything after it as a match.

Enjoy!

Dave
0
 

Author Comment

by:mato01
ID: 36486612
I mistyped.  I meant in Column A, not Cell A1.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36486624
So, you're looking for that text anywhere in column A?

Dave
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!

 
LVL 42

Expert Comment

by:dlmille
ID: 36486655
Ok - gotcha.

Here's the revised code - see documentation in the code for hints on tweaking this:
 
Sub checkA1toClear()
Dim ws As Worksheet
Dim wk As Workbook
Dim fRange As Range
Dim lastRow As Long

    Const strMatch = " Change in File and delete that row and every row after."

    Set wk = ThisWorkbook 'or could be activeworkbook if your running against another
    
    
    For Each ws In wk.Worksheets
    
        Set fRange = ws.Range("A:A").Find(What:=strMatch, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
        'ws.Range("A:A").Find = means to look in column A
        'Lookin:=xlValues = means it won't look in formulas for the string, but constants.  xlFormulas for formula related searches
        'Lookat:=xlPart = means if strMatch is found in any part of any cell, otherwise use xlWhole for an exact match in a cell
        'MatchCase:=False = means non case-sensitive search, otherwise use True for case-sensitive searches
        'SearchOrder:=xlByRows = means to look row by row
        
        If Not fRange Is Nothing Then 'found it
            lastRow = ws.UsedRange.Row
            ws.Range(fRange, ws.Range("A" & lastRow)).EntireRow.Clear 'clears the entire row
            'use .Delete to delete the entire row
        End If
    Next ws
End Sub

Open in new window


Enjoy!

Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 50 total points
ID: 36486688
Ouch - that was sloppy.  I needed to revise lines 22/23.  UsedRange doesn't necessarily include initial rows at the top that might not be used, so it would not be deleting correctly.

Here's the revised code:
 
Sub checkA1toClear()
Dim ws As Worksheet
Dim wk As Workbook
Dim fRange As Range
Dim fLastRow As Range

    Const strMatch = " Change in File and delete that row and every row after."

    Set wk = ThisWorkbook 'or could be activeworkbook if your running against another
    
    
    For Each ws In wk.Worksheets
    
        Set fRange = ws.Range("A:A").Find(what:=strMatch, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
        'ws.Range("A:A").Find = means to look in column A
        'Lookin:=xlValues = means it won't look in formulas for the string, but constants.  xlFormulas for formula related searches
        'Lookat:=xlPart = means if strMatch is found in any part of any cell, otherwise use xlWhole for an exact match in a cell
        'MatchCase:=False = means non case-sensitive search, otherwise use True for case-sensitive searches
        'SearchOrder:=xlByRows = means to look row by row
        
        If Not fRange Is Nothing Then 'found it
            Set fLastRow = ws.Cells.Find(what:="*", SearchDirection:=xlPrevious) 'search for last row, looking from the bottom, upward
            ws.Range(fRange, ws.Range("A" & fLastRow.Row)).EntireRow.Clear 'clears the entire row
            'use .Delete to delete the entire row
        End If
    Next ws
End Sub

Open in new window


Hope this helps!

Dave
0
 

Author Comment

by:mato01
ID: 36486713
Nothing happened.

Pasted the value in the string

    Const strMatch = " * Indicates change in information shown"



0
 
LVL 42

Expert Comment

by:dlmille
ID: 36486721
Here's a working example.

Does this text string occur in column A?  are there any special characters in that string?

If you just did an Excel Find from the menu, are you able to find that string by pasting it in and hitting "Find"?

See attached...

Dave
findTextandDeletetoLastRow-r1.xlsm
0
 

Author Closing Comment

by:mato01
ID: 36486785
Thanks.  Works fine.  I just needed to change to ActiveWorkbook,
0

Featured Post

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!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

679 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