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

x
?
Solved

Go to Specific Cell and Delete all rows after.

Posted on 2011-09-05
8
Medium Priority
?
376 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

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.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

916 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