Results 1 to 10 of 10

Thread: Clear Specific Rows Of Data On Multiple WorkSheets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    14
    Hi Charles

    Attached please find spreadsheet with Data

    I need a macro to do clear all the data from row 4 inColumns A:B & D up to the row before where total appears

    Regards

    Howard
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Howard,

    Using your attachment, I have put together the following:

    Code:
    Sub HowardsClearRoutine()
    
    Const FirstRowToClear = 4
    Const FirstColumnToClear = "A"
    Const LastColumnToClear = "D"
    Const TotalColumn = "A"
    
    Dim LastRowToClear As Integer
    Dim RangeToClear As String
    Dim LocationOfTotal As Range
    
    '   Find the row where the word "Total" is in column A, and clearthe contents of rows
    '   FirstRowToClear through LastRowToClear for columns FirstColumnToClear thru LastColumnToClear
    Set Sht = ActiveSheet       '   You may want to be a little more specific about this!
    
    Set LocationOfTotal = Sht.Columns(TotalColumn).Find("Total")
    '   Last row to clear is the row BEFORE the Total
    LastRowToClear = LocationOfTotal.Row - 1
    '   Now construct the range which we will be clearing
    RangeToClear = FirstColumnToClear + Format(FirstRowToClear)
    RangeToClear = RangeToClear + ":"
    RangeToClear = RangeToClear + LastColumnToClear + Format(LastRowToClear)
    '   Do we want to clear the contents, or delete the cells? This example simply clears the contents
    Sht.Range(RangeToClear).ClearContents
    
    
    End Sub
    A couple of points. It's ALWAYS better to use constants rather than magic numbers. If you have to go through the code when an extra header row is added,
    you have to remember the importance of every single "4" in your code. Imagine if you had NumHeaderRows, NumSeasons, NumDivisionsIn AFC etc. Not only is the code easier to read, but it makes you think a little bit more about it, as you are typing it in!

    The task is basically, construct a range which represents the cells you want to clear. That's really all this example does. It does it very slowly (so you can see how the range grows!). If you simply want to clear the cells (which is what I have done, to preserve the test data ;-) it will work as is. If you want to delete the cells (you did indicate that the location of the Total may change), then you should substitute Sht.Range(RangeToClear).Delete (xlShiftUp). You use xlShiftUp to make certain that the
    cells that used to contain data disappear, without destroying that nifty little look-up area you have.

    HTH

    Tony

Similar Threads

  1. Save Worksheets As New File To Specific Folder
    By k0st4din in forum Excel Help
    Replies: 18
    Last Post: 06-08-2013, 04:24 PM
  2. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  3. Macro To Clear Certain Data Across Many Sheets
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 12-04-2012, 09:10 AM
  4. Macro to clear data based on color fill
    By Howardc in forum Excel Help
    Replies: 7
    Last Post: 12-03-2012, 09:25 AM
  5. Replies: 2
    Last Post: 06-14-2012, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •