Here is my challenge. I try to copy and paste data based on Currency and Dates to a newly created Worksheets in the same Workbook. Each newly created Worksheet should be named by Currency and Date from the main source. I am stuck with dates and I am not sure how to add other currencies. Please advise. Thank you very much.
Option Explicit
Sub Create_Copy_of_JE_DATA_Split_By_Currency_AND_By_Date()
Dim draft As Worksheet
Dim curr_date As Worksheet
Dim LastRow
Dim LastColumn As Integer
Dim i
Dim drafttable As Object
Dim Curr As String
Dim transdate As Date
'Clean up previous data before start the macro Application.DisplayAlerts = False For Each i In ActiveWorkbook.Worksheets If i.name = "Draft_Data" Then i.Delete Next i
For Each i In ActiveWorkbook.Worksheets
If i.name = "Currency_Date" Then i.Delete
Next i
Application.DisplayAlerts = True
'Create a draft sheet to work with data Sheets("JE_data").Select Sheets("JE_data").Copy After:=Sheets(Sheets.count)
ActiveSheet.name = "Draft_Data"
Set draft = Sheets("Draft_Data")
LastRow = draft.Range("A1").End(xlDown).End(xlDown).End(xlUp).Row
LastColumn = draft.Range("A1").End(xlToRight).Column
'Copy Currency and Date data to find unique data 'It depends on your data structure, the original assumption was that column C is currency and column D is transaction Date 'The actual data structure is different - Currency is Column "P" and Date is Column "W", 'so I would have to delete Columns between them
Range("P2:W" & LastRow).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.count)
ActiveSheet.name = "Currency_Date"
Set curr_date = Sheets("Currency_Date")
ActiveSheet.Paste
Application.CutCopyMode = False
With Sheets("Currency_Date")
.Columns("B:G").EntireColumn.Delete
.Range("$A$1:$B$" & LastRow).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
End With
'ActiveSheet.Range("$A$1:$B$" & LastRow).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
'Select Draft sheet and start filtering draft.Select ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$W$" & LastRow), , xlYes).name = "Draft_table"
'so when I filter it, it will have the same format.
'it's upto you to choose the date format, :) I'm in Australia so I choose d/mm/yyyy
Columns("W:W").Select
Selection.NumberFormat = "d/mm/yyyy;@"
Set drafttable = draft.ListObjects("Draft_table")
'The idea is for each unique value of currency and date pair, we will filter this Draft 'table
'and copy the result to a new sheet then rename this sheet.
For i = 1 To Sheets("Currency_Date").Range("A1").End(xlDown).End(xlDown).End(xlUp).Row
Curr = curr_date.Range("A" & i).Value
transdate = curr_date.Range("B" & i).Value
draft.Select
drafttable.Range.AutoFilter Field:=16, Criteria1:=Curr
drafttable.Range.AutoFilter Field:=23, Criteria1:=transdate
drafttable.Range.AutoFilter Field:=23, Criteria1:="=" & transdate, Operator:=xlAnd
Range("Draft_table").SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.count)
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ActiveSheet.name = Format(transdate, "MMM DD YYYY") & " " & Curr
Sheets("JE_Data").Select
Rows("1:1").Select
Selection.Copy
Sheets(Format(transdate, "MMM DD YYYY") & " " & Curr).Select
Rows("1:1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.EntireColumn.AutoFit
'Prepare for next filter.
draft.ShowAllData
Next i
'draft.Delete 'curr_date.Delete
End Sub