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

x
?
Solved

Go to Specific Cell and Delete all rows after.

Posted on 2011-09-05
8
Medium Priority
?
373 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 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