Help In Fixing VBA Code: Code Debugging

Flani

New Member
Joined
Jul 11, 2012
Messages
17
Code:
Option Explicit


Sub CopyDeletedAdjs()
Dim ws As Worksheet, wsMaster As Worksheet
Dim lr As Long, NR As Long


Sheets("Deleted Adjs").Select
Set wsMaster = Sheets("Deleted Adjs")
                                            'Delete Raw Data contents?
If MsgBox("Clear the Deleted Adjustments tab first?", vbYesNo, "Reset Deleted Adjustments tab?") = vbYes Then
  Range("A2:DB65000").Select
    Selection.ClearContents
    'wsMaster.UsedRange.Offset(1).Clear      'clears all but row1, the titles
    NR = 2                                  'Next Row to paste into is row2
Else
    NR = wsMaster.Range("A" & Rows.Count).End(xlUp).Row + 1 'find the next empty row to paste into
End If
                                            'process the listed sheets one at a time
For Each ws In Sheets(Array("Day1", "Empty"))
    With ws                                 'all commands go to the current 'ws' from the array
        .AutoFilterMode = False             'turn off prior autofilters
        .Rows(1).autofilter                 'turn on a clean new autofilter
        .Rows(1).autofilter Field:=12, Criteria1:= _
        "=*Adjustment deleted*", Operator:=xlOr, Criteria2:= _
        "=*Reversal adjustment*"  'filter column B
        lr = .Range("A" & .Rows.Count).End(xlUp).Row        'are any rows still visible?
        If lr > 1 Then                                      'if so, copy them all to the Deleted adjs
            .Range("A2:A" & lr).EntireRow.Copy wsMaster.Range("A" & NR)
            NR = wsMaster.Range("A" & Rows.Count).End(xlUp).Row + 1 'find next empty row on deleted adjs
        End If
        .AutoFilterMode = False             'turn off the autofilter
    End With
Next ws                                     'go to the next sheet listed in the array and repeat


End Sub

I have a problem with the code above:

1. i need to remove the message box and condition before deleting the contents of deleted adjs and instead just deleting everything instead.
2. instead of deleting range A2:DB65000, delete all filled rows instead.
3. This: "For Each ws In Sheets(Array("Day1", "Empty"))" Empty worksheet has been deleted already. so no need for sheets array
4. The code below works fine if there are deleted/reversal adjustment for the day. But when there are no deleted adjustments the code debugs and the macro would not work. This is in the midway of the process. Please help
Code:
With ws                                 'all commands go to the current 'ws' from the array
        .AutoFilterMode = False             'turn off prior autofilters
        .Rows(1).autofilter                 'turn on a clean new autofilter
        .Rows(1).autofilter Field:=12, Criteria1:= _
        "=*Adjustment deleted*", Operator:=xlOr, Criteria2:= _
        "=*Reversal adjustment*"  'filter column B
        lr = .Range("A" & .Rows.Count).End(xlUp).Row        'are any rows still visible?
        If lr > 1 Then                                      'if so, copy them all to the Deleted adjs
            .Range("A2:A" & lr).EntireRow.Copy wsMaster.Range("A" & NR)
            NR = wsMaster.Range("A" & Rows.Count).End(xlUp).Row + 1 'find next empty row on deleted adjs
        End If
        .AutoFilterMode = False             'turn off the autofilter
    End With
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,203,453
Messages
6,055,533
Members
444,794
Latest member
HSAL

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