Combining two codes for two different sheets into a mastercode on a separate sheet

glockgal42

New Member
Joined
Dec 15, 2016
Messages
5
So I have two codes that I am using to delete cells from two different sheets. I first had a button on each sheet to run the macros, but instead I would like to create one button on my third sheet that will run both macros.

My first code is for Worksheet ("yard")

Code:
Sub RemoveTractors()
    Last = Cells(Rows.Count, "B").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "B").Value) = "Tractor" Then
            Cells(i, "A").EntireRow.Delete
        End If
        Next i
    End Sub

My second code is for Worksheet ("Yard Configuration")

Code:
Sub RemoveDeletedLocations()
    Last = Cells(Rows.Count, "K").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "K").Value) = "Yes" Then
            Cells(i, "A").EntireRow.Delete
        End If
        Next i
    End Sub

Then my master code will be placed on Worksheet ("Yard Availabilities")

Code:
Sub FormatLists()
    Call RemoveTractors
    Call RemoveDeletedLocations
End Sub

What's the best way to go about this?
Thank you!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
you will have to be specific about the sheets inside the individual macros. If you do not list a sheet, it will assume you are talking about the active sheet... in your case "Yard Availabilities".. which is not where you want the macro to be done..

try this

Code:
[COLOR=#333333]Sub RemoveTractors()
    [COLOR=#ff0000]With Sheets("yard")[/COLOR]
        Last = [COLOR=#ff0000].[/COLOR]Cells([COLOR=#ff0000].[/COLOR]Rows.Count, "B").End(xlUp).Row
        For i = Last To 1 Step -1
            If ([COLOR=#ff0000].[/COLOR]Cells(i, "B").Value) = "Tractor" Then
                [COLOR=#ff0000].[/COLOR]Cells(i, "A").EntireRow.Delete
            End If
        Next i
    [COLOR=#ff0000]End With [/COLOR]
End Sub
[/COLOR]
[COLOR=#333333]Sub RemoveDeletedLocations()
    [COLOR=#ff0000]With Sheets("Yard Configuration")[/COLOR]
         Last = [COLOR=#ff0000].[/COLOR]Cells([COLOR=#ff0000].[/COLOR]Rows.Count, "K").End(xlUp).Row
         For i = Last To 1 Step -1
             If ([COLOR=#ff0000].[/COLOR]Cells(i, "K").Value) = "Yes" Then
                 [COLOR=#ff0000].[/COLOR]Cells(i, "A").EntireRow.Delete
             End If
         Next i
    [COLOR=#ff0000]End With[/COLOR]
End Sub
[/COLOR]
[COLOR=#333333]Sub FormatLists()
    Call RemoveTractors
    Call RemoveDeletedLocations
End Sub[/COLOR]
 
Last edited:
Upvote 0
See I tried that but it looks like I did not add the . in front of cells or rows so that could have been my problem. Thank you! I will try that.
 
Upvote 0
Using a With statement keeps something in its "memory".. so when you start a call with a dot, it means that the blank space before the dot is the entire thing inside your with statement.

So if you are on sheet, Yard...

With Sheets("Yard Configuration")
Range("A1").Value
End With

is going to be the A1 value in sheet, Yard (because you have not set the sheet... it assumes that you mean the active sheet, Yard.)

Whereas

With Sheets("Yard Configuration")
.Range("A1").Value
End With

will be the A1 value on the sheet, Yard Configuration. The dot connects the Range to the sheet you assigned to the With statement.


I hope this helps make things clear..
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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