Looping through sheets excluding sheet name isn't working

amartin575

New Member
Joined
Nov 3, 2016
Messages
8
Hello,

I have the code below which is supposed to loop through all sheets in the workbook except for the one labeled CONSOLIDATED DATA or (Sheet1). It then does a reverse find on column A looking in reverse order for labels that form a table towards the bottom of the data. Once it finds the last label which would be the top of the table it moves up one row and deletes all rows above what it found. It will then clean up the tab some more once I get the code working right. Once that is done it is supposed to move to the next tab and do the same thing until it hits all tabs but the first one. If I run the code on its own on one tab without the loop it works fine. With the loop if I click run from the CONSOLIDATED DATA tab it ignores the exclusion and alters the tab I don't want touched. If I run the loop on one of the other tabs it runs it on that tab. Once it updates the tab it doesn't move at all and tries to run the code on the tab that was just altered and ends up failing at ActiveCell.Offset (-1,0) because at that point the active cell is A1 and it can't move up a -1 from A1. Any idea why the loop isn't moving across all sheets but Sheet1?

Code:
Sub FindFromTheBottom()

Dim ws As Worksheet
Dim LastRow As Long
Dim a As Range


    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "CONSOLIDATED DATA" Then
            With ws
                On Error Resume Next
                Set a = .Range("A:A").Find("Strip Center", after:=Cells(1, 1), searchdirection:=xlPrevious)
                a.Cells.Select
                On Error GoTo 0
                On Error Resume Next
                Set a = .Range("A:A").Find("Office Apartment", after:=Cells(1, 1), searchdirection:=xlPrevious)
                a.Cells.Select
                On Error GoTo 0
                On Error Resume Next
                Set a = .Range("A:A").Find("Inline", after:=Cells(1, 1), searchdirection:=xlPrevious)
                a.Cells.Select
                On Error GoTo 0
                On Error Resume Next
                Set a = .Range("A:A").Find("Outparcel", after:=Cells(1, 1), searchdirection:=xlPrevious)
                a.Cells.Select
                On Error GoTo 0
                On Error Resume Next
                Set a = .Range("A:A").Find("Anchor", after:=Cells(1, 1), searchdirection:=xlPrevious)
                a.Cells.Select
                On Error GoTo 0


                ActiveCell.Offset(-1, 0).Select
                Range(Selection, Cells(1)).Select
                Selection.EntireRow.Delete
                Columns("A:A").Select
                Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                Range("A1").Select
                ActiveCell.FormulaR1C1 = _
                "=MID(CELL(""filename"",RC),FIND(""]"",CELL(""filename"",RC))+1,255)"
 
                LastRow = .Range("C" & Rows.Count).End(xlUp).Row
                .Range("A1").Copy .Range("A1:A" & LastRow)


            End With
        End If
    Next ws


End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Some of your code is working on the current sheet. For example, ActiveCell is the active cell on the current sheet. Without refactoring everything else, you could put this before the first "On Error" line:

Code:
ws.Activate

WBD
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top