I still have NO idea what step 5 does as it is not clear on your example but here is what I created:
MDDataExtract.xlsm
Option Explicit
Sub ProcessMyData()
'===================================================================
'Declare Variables
'===================================================================
Dim DataSheet As String 'Used to define DataSource sheet
Dim CreditsSheet As String 'Used to define Credit sheet
Dim ws As Worksheet 'Used to create new worksheet
Dim LastRow As Long 'Used to determine last row with data
Dim I As Long 'Used to loop through rows
'===================================================================
'Define Variables
'===================================================================
DataSheet = "MDDataExtract" 'Defines the Data Sheet Name
CreditsSheet = "Credits" 'Defines the Credits Sheet Name
LastRow = Worksheets(DataSheet).Cells(Rows.Count, 1).End(xlUp).Row 'This will find the last used row in column A of Data sheet
'===================================================================
'Setup for speed
'===================================================================
Application.ScreenUpdating = False 'Sets ScreenUpdating Off
Application.Calculation = xlCalculationManual 'Sets calculations to manual so it wont calc during procedute
'===================================================================
'Process Data
'===================================================================
'Step 1
ThisWorkbook.Worksheets(DataSheet).Range("A:C, G:H, M:AB, AE:AK").Delete ' Deletes the Columns
'Step 2 - Create worksheet then copy paste Negative Order Quantities
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) 'Add Worksheet
ws.Name = CreditsSheet 'Renames Worksheet
'Filter on Negative Column O values
ThisWorkbook.Worksheets(DataSheet).Range("A:Q").AutoFilter Field:=15, Criteria1:="<0" 'Applies filter on Column 0 for anything less than 0
ThisWorkbook.Worksheets(DataSheet).Range("A1:Q" & LastRow).SpecialCells(xlCellTypeVisible).Copy
ThisWorkbook.Worksheets(CreditsSheet).Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False 'paste visible cells only to Credits tab
Application.CutCopyMode = False
'Step 3 - Delete Negative Quantities
ThisWorkbook.Worksheets(DataSheet).Range("A2:Q" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete 'Deletes Copied Data
ThisWorkbook.Worksheets(DataSheet).Cells.AutoFilter 'Removes Autofilter
'Step 4 - Sort
LastRow = Worksheets(DataSheet).Cells(Rows.Count, 1).End(xlUp).Row 'finds the last used row in column A of Data sheet
ThisWorkbook.Worksheets(DataSheet).Sort.SortFields.Clear ' clears current sorting
ThisWorkbook.Worksheets(DataSheet).Sort.SortFields.Add Key:=Range("C2:C" & LastRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Sets sort method for column C
ThisWorkbook.Worksheets(DataSheet).Sort.SortFields.Add Key:=Range("A2:A" & LastRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Sets sort method for column A
ThisWorkbook.Worksheets(DataSheet).Sort.SortFields.Add Key:=Range("J2:J" & LastRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Sets sort method for column J
With ThisWorkbook.Worksheets(DataSheet).Sort
.SetRange Range("A1:Q" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With 'Applies Sort
'Step 5 -Clear Contents in Column O and rename Header
'Range("O1").Value = "Quantity"
'I have no idea what you want for Step 5, it just doesn't make sense
'Step 6 - Loop through all cells in Column C to Insert Rows when product name changes
ThisWorkbook.Worksheets(DataSheet).Activate
For I = LastRow To 3 Step -1 'Starting at the last cell in column C and going up the worksheet one row at a time
If Range("C" & I).Value <> Range("C" & I - 1) And Range("C" & I - 1) <> "" Then 'If cell is NOT the same as the cell above it AND cell above is NOT blank then
Rows(I & ":" & I + 1).EntireRow.Insert 'Inserts 2 rows
End If
Next I 'Go to the next row up
ThisWorkbook.Worksheets(DataSheet).Range("A1").Select 'Just selects a random cell to tidy things up at the end
'===================================================================
'End Macro Procedures
'===================================================================
ThisWorkbook.Worksheets("Process").Activate 'Activates this tab, to tidy things up
Range("A1").Select ' Just to return to a fixed point, why not
Application.ScreenUpdating = True 'Turns screen updating back on
Application.Calculation = xlCalculationAutomatic 'Turns calculations back on
MsgBox "Process Complete" 'Gives the user a message
End Sub
Sub Reset()
Dim ws As Worksheet
'===================================================================
'Setup for speed
'===================================================================
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'===================================================================
'Do Work
'===================================================================
For Each ws In Worksheets
If UCase(ws.Name) = "CREDITS" Then
ws.Delete
End If
Next ws
'Add code to delete Credits tab?
Sheets("MDDataExtract - Original").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("MDDataExtract").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
If ActiveSheet.AutoFilterMode Then
Cells.AutoFilter
End If
'===================================================================
'End Macro Procedures
'===================================================================
ThisWorkbook.Worksheets("Process").Activate
Range("A1").Select
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
Bookmarks