Option Explicit
Sub SPLIT_SHEETS()
Application.SheetsInNewWorkbook = 1
Dim key_col As Integer, wb_sh_split As Integer
Dim last_col_descr As String, rng_col_letter As String, sheet_name As String, del_col As String
If ActiveSheet.AutoFilterMode Then
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
Debug.Print ActiveWorkbook.name & ". " & ActiveSheet.name & ". Filter has been cleared"
End If
End If
last_col_descr = "Change Number"
del_col = "KEY"
Cells.Find(what:=last_col_descr, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
rng_col_letter = Split(ActiveCell(1).Address(1, 0), "$")(0)
Cells.Find(what:=del_col, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
key_col = ActiveCell.column
sheet_name = ActiveSheet.name
Debug.Print sheet_name; key_col
wb_sh_split = MsgBox("Do you want to split data to workbooks? Yes to workbooks, No to sheets", vbYesNoCancel, "Please make your choise.")
If wb_sh_split = vbYes Then
wb_sh_split = 1
Call create_subdir
ElseIf wb_sh_split = vbNo Then
wb_sh_split = 2
ElseIf wb_sh_split = vbCancel Then
Exit Sub
End If
Call SPLIT_SHEETS_CORE(rng_col_letter, sheet_name, del_col, key_col, wb_sh_split)
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
ActiveWorkbook.Save
End Sub
Function SPLIT_SHEETS_CORE(rng_col_letter As String, sheet_name As String, del_column As String, vcol As Integer, wb_sh_split As Integer)
Dim icol As Long, lr As Long
Dim ws As Worksheet
Dim wb As Workbook
Dim dest_wb As Workbook
Dim titlerow As Long, i As Long
Dim myarr As Variant
Dim new_sh_name As String
Dim strdir As String, title As String
'vcol = 1 'vcol =1, the number 1 is the column number that you want to split the data based on.
strdir = ActiveWorkbook.Path & "\" & "Splitted" & "\"
Set ws = Sheets(sheet_name)
new_sh_name = "Consolidated_file"
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).row
title = "A1:" & rng_col_letter & "1"
titlerow = ws.Range(title).Cells(1).row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
If wb_sh_split = 1 Then
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Debug.Print "1"
With Workbooks.Add
With .Sheets.Add(Before:=.Sheets(1))
.name = new_sh_name
End With
End With
Application.DisplayAlerts = False
ActiveWorkbook.Worksheets("Sheet1").Delete
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs Filename:=strdir & myarr(i) & ""
Else
Debug.Print "2"
With Workbooks.Add
With .Sheets.Add(After:=Worksheets(Worksheets.Count))
.name = new_sh_name
End With
End With
End If
DoEvents
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(new_sh_name).Range("A1")
Sheets(new_sh_name).Columns.AutoFit
Call match_and_delete(del_column)
ActiveWorkbook.Close SaveChanges:=True
Next
ElseIf wb_sh_split = 2 Then
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(After:=Worksheets(Worksheets.Count)).name = new_sh_name
Else
Sheets(new_sh_name).Move After:=Worksheets(Worksheets.Count)
End If
DoEvents
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(new_sh_name).Range("A1")
Sheets(new_sh_name).Columns.AutoFit
Next
End If
ws.AutoFilterMode = False
ws.Activate
End Function
Function create_subdir()
Dim strdir As String
strdir = ActiveWorkbook.Path & "\" & "Splitted" & "\"
If Dir(strdir, vbDirectory) = "" Then
MkDir strdir
Else
End If
End Function
Function match_and_delete(col_name As String)
Dim i As Integer
On Error GoTo ColumnNotExist
i = Application.WorksheetFunction.Match(col_name, Range("A1:AZ1"), 0)
If i > 0 Then
Debug.Print ActiveWorkbook.name & "Column number is " & i
Cells.Find(what:=col_name, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select
Selection.EntireColumn.Delete
Debug.Print ActiveWorkbook.name & ". Column (" & col_name & ") has been deleted."
End If
Exit Function
ColumnNotExist:
Debug.Print ActiveWorkbook.name & ". Column (" & col_name & ") does not exist and nothing has been done."
Err.Clear
End Function