Combining two subs = broken functionality

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
243
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Here's my two subs

VBA Code:
Sub Sort1() 'Initial sort

    Dim ws As Worksheet
    Dim rng As Range
    
    Set ws = Worksheets(2)

    With ws
        'Formats times
        .Columns("K:L").NumberFormat = "hh:mm"
        'Removes entries with no data in A
        On Error Resume Next
        ws.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        On Error GoTo 0
        'Removes entries with no data in M
        On Error Resume Next
        ws.Columns("M").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        On Error GoTo 0
    End With
    
End Sub
Sub BreakFixer() 

    Dim ws As Worksheet
    Dim rng As Range
    
    Set ws = Worksheets(2)
    Set rng = ws.Range("M1", ws.Range("M1").End(xlDown))
    
    With ws
        .Range("R1").Formula2R1C1 = _
            "=CEILING(RC[-7]:INDEX(C[-7],COUNTA(C[-7]))-TIME(0,7,30),TIME(0,15,0))"
        .Range("R1", ws.Range("R1").End(xlDown)).Copy
        .Range("K1").PasteSpecial xlPasteValues
        .Range("R1", ws.Range("R1").End(xlDown)).Clear
        For Each Cell In rng
            If Cell.Value = "Br" Then Cell.Offset(1, -2).Value = Cell.Offset(0, -2).Value + TimeValue("00:15:00")
        Next
    End With

End Sub

Both subs work exactly as intended when ran separately. However, when I combine them as below the function of the second sub halts and I'm not sure why.. though I'm suspicious of it being something to do with the GoTo 0 error handling lines. No error is thrown when the combined macro runs, it just doesn't do as its told.

VBA Code:
Sub Sort1() 'Initial sort of All Activity

    Dim ws As Worksheet
    Dim rng As Range
    
    Set ws = Worksheets(2)
    Set rng = ws.Range("M1", ws.Range("M1").End(xlDown))

    With ws
        'Formats start and end times
        .Columns("K:L").NumberFormat = "hh:mm"
        'Removes entries with no Employee Number
        On Error Resume Next
        ws.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        On Error GoTo 0
        'Removes entries with no activity/absence codes
        On Error Resume Next
        ws.Columns("M").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        On Error GoTo 0
        .Range("R1").Formula2R1C1 = _
            "=CEILING(RC[-7]:INDEX(C[-7],COUNTA(C[-7]))-TIME(0,7,30),TIME(0,15,0))"
        .Range("R1", ws.Range("R1").End(xlDown)).Copy
        .Range("K1").PasteSpecial xlPasteValues
        .Range("R1", ws.Range("R1").End(xlDown)).Clear
        For Each Cell In rng
            If Cell.Value = "Br" Then Cell.Offset(1, -2).Value = Cell.Offset(0, -2).Value + TimeValue("00:15:00")
        Next
    End With
    
End Sub

Any suggestions?

Thanks!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try stepping your code one line at a time using the F8 key, and you can see exactly how it is progresses and where it kicks out.
That should help you identify what is going on and where and why the error or unexpected behavior is occurring.
(This is a great debugging technique when things don't go as planned).
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,042
Members
448,940
Latest member
mdusw

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