Solved

Go to Specific Cell and Delete all rows after.

Posted on 2011-09-05
8
344 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 41

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 41

Expert Comment

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

Dave
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 41

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 41

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 41

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

786 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