Run Macro on Two worksheet

aaronr

New Member
Joined
Jul 8, 2010
Messages
34
I'm have issue running this code. I keep getting getting a compile error: for without Next. Can you guys please help.

Sub DeleteRows_BASEMENT(IRibbonControl)
Dim sht As Worksheet, myShts As Variant
myShts = Array("PARENT VIEWS", "Child Sheet Index") '<-- list all the sheet names you want the code to run on here
For i = LBound(myShts) To UBound(myShts)
If i = LBound(myShts) Then
Sheets(myShts(i)).Select Replace:=True
Else
Sheets(myShts(i)).Select Replace:=False
End If
For Each sht In ActiveWindow.SelectedSheets
Last = Cells(Rows.Count, "D").End(xlUp).Row
For a = Last To 1 Step -1
If (Cells(a, "D").Value) Like "*BASEMENT*" Then
Cells(a, "D").EntireRow.Delete
Exit For
End If
Next a
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This is why indenting helps as it showed me you were missing two next's i.e. Next i and Next sht:

Code:
Sub DeleteRows_BASEMENT(IRibbonControl)
    Dim sht As Worksheet, myShts As Variant
    myShts = Array("PARENT VIEWS", "Child Sheet Index") '<-- list all the sheet names you want the code to run on here
    For i = LBound(myShts) To UBound(myShts)
        If i = LBound(myShts) Then
            Sheets(myShts(i)).Select Replace:=True
        Else
            Sheets(myShts(i)).Select Replace:=False
        End If
    Next i
    For Each sht In ActiveWindow.SelectedSheets
        Last = Cells(Rows.Count, "D").End(xlUp).Row
        For a = Last To 1 Step -1
            If (Cells(a, "D").Value) Like "*BASEMENT*" Then
                Cells(a, "D").EntireRow.Delete
                Exit For
            End If
        Next a
    Next sht
End Sub

HTH

Robert
 
Upvote 0
Robert thanks for your help. I see What you mean about the indenting. When I run the macro it leaves the tabs highlight. Is there a way added code to unhighlight the tabs? Thanks again
 
Upvote 0
Is there a way added code to unhighlight the tabs

Selecting one of tabs would do it i.e.

Code:
Sheets("PARENT VIEWS").Select
 
Upvote 0
OK. i'm not sure what I'm missing. It delete the row with Basement on Parent Views but it doesn't do the Child Sheet Index.


This is why indenting helps as it showed me you were missing two next's i.e. Next i and Next sht:

Code:
Sub DeleteRows_BASEMENT(IRibbonControl)
    Dim sht As Worksheet, myShts As Variant
    myShts = Array("PARENT VIEWS", "Child Sheet Index") '<-- list all the sheet names you want the code to run on here
    For i = LBound(myShts) To UBound(myShts)
        If i = LBound(myShts) Then
            Sheets(myShts(i)).Select Replace:=True
        Else
            Sheets(myShts(i)).Select Replace:=False
        End If
    Next i
    For Each sht In ActiveWindow.SelectedSheets
        Last = Cells(Rows.Count, "D").End(xlUp).Row
        For a = Last To 1 Step -1
            If (Cells(a, "D").Value) Like "*BASEMENT*" Then
                Cells(a, "D").EntireRow.Delete
                Exit For
            End If
        Next a
    Next sht
End Sub

HTH

Robert
 
Upvote 0
It delete the row with Basement on Parent Views but it doesn't do the Child Sheet Index.

There's a lot of code just for that :confused:

I'm not sure what the IRibbonControl parameter is for but try this (I've added some notes):

Code:
Sub DeleteRows_BASEMENT(IRibbonControl)

    Dim wsMySheet As Worksheet
    Dim lngMyRow  As Long
    
    Application.ScreenUpdating = False
    
    For Each wsMySheet In Sheets(Array("PARENT VIEWS", "Child Sheet Index"))
        For lngMyRow = wsMySheet.Cells(Rows.Count, "D").End(xlUp).Row To 1 Step -1
            If wsMySheet.Range("D" & lngMyRow).Value Like "*BASEMENT*" Then 'Case sensitive
                wsMySheet.Rows(lngMyRow).EntireRow.Delete
                Exit For 'Quits after first deletion
            End If
        Next lngMyRow
    Next wsMySheet
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
It worked like a Charm after i delete this line (Exit For 'Quits after first deletion) from the code.

I thought that line from your original code was odd - that's why I wrote the comment next to it :confused:

Glad we got it sorted in the end.

Robert
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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