Results 1 to 3 of 3

Thread: Macro To Clear Certain Data Across Many Sheets

  1. #1
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13

    Macro To Clear Certain Data Across Many Sheets

    I have written code to clear data from row 4 onwards and to include columns A:B, C:D only and to ignore the last three sheets

    I only need to data clear up to o1 row before where "Total" appears in Col A



    However, when activating the code , I get a message compile error: Invalid next control variance reference and next I is highlighted

    Your assistance in resolving this is most appreciated






    Code:
    Sub Clear_data()
    Dim i As Long
    For i = 1 To Worksheets.Count - 3
    
    Dim MyBook As Workbook
    Dim Sht As Worksheet
    Dim RangeToErase As Range
    Dim YTDTotal As Range
    Set MyBook = ThisWorkbook
    For Each Sht In MyBook.Worksheets
    Set YTDTotal = Sht.Range("A:A").Find("Total")
     With Worksheets(i)
            LR = .Range("A" & Rows.Count).End(xlUp).Row
            End With
            If Not YTDTotal Is Nothing Then
                Sht.Range("A4:b" & YTDTotal.Row - 1).Clear
                Sht.Range("D4:D" & YTDTotal.Row - 1).Clear
            End If
            Next i
    Next Sht
    End Sub
    Last edited by Admin; 12-04-2012 at 09:10 AM. Reason: code tag added

  2. #2
    Member rollis13's Avatar
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0
    That is because the "Next i" must be after "Next Sht" and not before.

  3. #3
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    @ Howard please use code tags while posting codes
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 7
    Last Post: 05-08-2013, 07:12 PM
  2. Replies: 9
    Last Post: 12-04-2012, 09:45 PM
  3. Macro to clear data based on color fill
    By Howardc in forum Excel Help
    Replies: 7
    Last Post: 12-03-2012, 09:25 AM
  4. Replies: 2
    Last Post: 11-08-2012, 01:15 PM
  5. Macro sending with mailto linkup to other sheets
    By ragil.yudy in forum Excel Help
    Replies: 2
    Last Post: 10-23-2012, 02:42 PM

Posting Permissions

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