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