Solved

Go to Specific Cell and Delete all rows after.

Posted on 2011-09-05
8
324 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now