There is a data, which has product codes A01 to A38, B01 to B38, C01 TO C38, D01 TO D38, we have to first filter A, B, C, D(done), create 4 workbooks in there name(done), Filter all product codes by removing duplicates(done), create worksheets with the name of those product codes like A01 to A38 in workbook A.xlsx, B01 TO B38 in B.xlsx and so on for C and D. Then copy corresponding data of Product codes A01 from the given excel file to worksheet A01 in workbook A.XLsx and same for all product codes under all apphabets B, C, D.
Everything should happen with the click of one macro button.
Please help. I have written the following macro, but they are all working separately.
#
Sub product_categoryQ1()
Workbooks.Open "C:\Users\preet\Downloads\DVA Case studies (except SQL)\VBA case studies\VBA Case Study 1\Sales Data.xlsx"
Worksheets("Sales Data").Activate
Range("A1").CurrentRegion.Select
Selection.Copy ThisWorkbook.Worksheets("Data").Range("A1")
Workbooks("Sales Data").Close False
End Sub
Sub product_categoryQ2()
Dim sourcerange As Range
Dim destinationrange As Range
Dim i As Integer
Dim lr As Long
lr = Range("C" & Rows.Count).End(xlUp).Row
Set sourcerange = Sheet1.Range("C2:C" & lr)
Set destinationrange = Sheet1.Range("J2:J" & lr)
For i = 1 To sourcerange.Count
destinationrange(i, 1).Value = Left(sourcerange(i, 1).Value, 1)
Next i
Columns("J:J").Select
ActiveSheet.Range("$J$1:$J$" & lr).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
Sub product_categoryQ3()
Dim j As Integer
Dim wb As Workbook
Dim filename As String
For j = 2 To 5
filename = ThisWorkbook.Worksheets("Data").Range("J" & j).Value
Set wb = Workbooks.Add
On Error Resume Next
Application.DisplayAlerts = False
wb.Delete
Application.DisplayAlerts = True
wb.SaveAs (filename & ".xlsx")
Next j
End Sub
Sub product_categoryQ4()
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("L2").Select
ActiveSheet.Paste
Columns("L:L").Select
ActiveSheet.Range("$L$1:$L$1092").RemoveDuplicates Columns:=1, Header:=xlNo
Range("L2", Range("L2").End(xlDown)).Sort Key1:=Range("L2"), Order1:=xlAscending, Header:=xlNo
Dim cell As Range
Dim wsA As Worksheet
Dim wbA As Workbook
Dim wsN As Worksheet
Dim rng As Range
Set wsA = ActiveSheet
Set wbA = Workbooks("A.xlsx")
For Each cell In wsA.Range("L2:L46")
With wbA
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
Workbooks("A.xlsx").ActiveSheet.Name = cell.Value
If Err.Number = 1004 Then
Debug.Print cell.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
With ActiveSheet
.Range("$A$1:$H$1092").AutoFilter Field:=3, Criteria1:=cell.Value
Set rng = ActiveSheet.AutoFilter.Range
rng.Copy Destination:=Workbooks("A.xlsx").Worksheets(cell.Value).Range("A1")
ActiveSheet.ShowAllData
End With
Next cell
Dim cellb As Range
Dim wsB As Worksheet
Dim wbB As Workbook
Dim rngb As Range
Set wsB = ActiveSheet
Set wbB = Workbooks("B.xlsx")
For Each cellb In wsB.Range("L47:L91")
With wbB
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
Workbooks("B.xlsx").ActiveSheet.Name = cellb.Value
If Err.Number = 1004 Then
Debug.Print cell.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
With ActiveSheet
.Range("$A$1:$H$1092").AutoFilter Field:=3, Criteria1:=cellb.Value
Set rngb = ActiveSheet.AutoFilter.Range
rngb.Copy Destination:=Workbooks("B.xlsx").Worksheets(cellb.Value).Range("A1")
ActiveSheet.ShowAllData
End With
Next cellb
Dim cellc As Range
Dim wsC As Worksheet
Dim wbC As Workbook
Dim rngc As Range
Set wsC = ActiveSheet
Set wbC = Workbooks("C.xlsx")
For Each cellc In wsC.Range("L92:L136")
With wbC
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
Workbooks("C.xlsx").ActiveSheet.Name = cellc.Value
If Err.Number = 1004 Then
Debug.Print cell.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
With ActiveSheet
.Range("$A$1:$H$1092").AutoFilter Field:=3, Criteria1:=cellc.Value
Set rngc = ActiveSheet.AutoFilter.Range
rngc.Copy Destination:=Workbooks("C.xlsx").Worksheets(cellc.Value).Range("A1")
ActiveSheet.ShowAllData
End With
Next cellc
Dim celld As Range
Dim wsD As Worksheet
Dim wbD As Workbook
Dim rngd As Range
Set wsD = ActiveSheet
Set wbD = Workbooks("D.xlsx")
For Each celld In wsD.Range("L137:L181")
With wbD
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
Workbooks("D.xlsx").ActiveSheet.Name = celld.Value
If Err.Number = 1004 Then
Debug.Print cell.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
With ActiveSheet
.Range("$A$1:$H$1092").AutoFilter Field:=3, Criteria1:=celld.Value
Set rngd = ActiveSheet.AutoFilter.Range
rngd.Copy Destination:=Workbooks("D.xlsx").Worksheets(celld.Value).Range("A1")
ActiveSheet.ShowAllData
End With
Next celld
End Sub
Bookmarks