Help on running two code auto when opening Excel workbook

Closed
Saeed1373 Posts 2 Registration date Tuesday August 2, 2016 Status Member Last seen August 3, 2016 - Aug 3, 2016 at 08:41 AM
TrowaD Posts 2913 Registration date Sunday September 12, 2010 Status Moderator Last seen November 21, 2022 - Aug 9, 2016 at 11:32 AM
Hello guys
i need some help about merging two or more code on vba
i used this code and it helps for only one code:

Private Sub Workbook_Open()
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
Set InputRng = Worksheets("Incident").Range("I:I")
DeleteStr = "aaa"
For Each rng In InputRng
If rng.Value = DeleteStr Then
If DeleteRng Is Nothing Then
Set DeleteRng = rng
Else
Set DeleteRng = Application.Union(DeleteRng, rng)
End If
End If
Next
DeleteRng.EntireRow.Delete
End Sub

when i open workbook it works
but i have more codes (like i sent earlier) that should run when i open the workbook, but the program doesnt work.
i would be really thank you if you help me about it.
the other code im trying to merge is:

Sub DeleteRows()
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
Set InputRng = Worksheets("Task").Range("I:I")
DeleteStr = "aaa"
For Each rng In InputRng
If rng.Value = DeleteStr Then
If DeleteRng Is Nothing Then
Set DeleteRng = rng
Else
Set DeleteRng = Application.Union(DeleteRng, rng)
End If
End If
Next
DeleteRng.EntireRow.Delete
End Sub

thank you very much!!!

1 reply

TrowaD Posts 2913 Registration date Sunday September 12, 2010 Status Moderator Last seen November 21, 2022 541
Aug 9, 2016 at 11:32 AM
Hi Saeed1373,

Well, you can basicly put them beneath each other or try something like this:
Private Sub Workbook_Open()
Dim rng, InputRng, DeleteRng As Range
Dim DeleteStr As String

Sheets("Incident").Select
TaskSheet:
Set InputRng = ActiveSheet.Range("I:I")
DeleteStr = "aaa"

For Each rng In InputRng
    If rng.Value = DeleteStr Then
        If DeleteRng Is Nothing Then
            Set DeleteRng = rng
        Else
            Set DeleteRng = Application.Union(DeleteRng, rng)
        End If
    End If
Next
DeleteRng.EntireRow.Delete

If ActiveSheet.Name = "Incident" Then
    Sheets("Task").Select
    GoTo TaskSheet
End If
End Sub


Best regards,
Trowa
0