I am having a little bit of trouble trying to split out some data. The macro I built I used some code that I found and altered it a little bit to do what I need it to do. I have 2 columns specifically that I am trying to use as the identifier to split out this data onto different sheets. The first column I need it to split by is column 16 (P), named "Agent". There is one specific "Agent" named "Direct" that can have various different companies in the next column, 17 (Q), which is the "Company" column. When it splits them all out, I would like the name it is using to split them out by to be entered as the worksheet name and include the word "Size Breaks" after it. The sheet I have as the template to transfer it to each time is called "SBD", that way it will go into the same format every time.
The code I have so far is below, but for some reason, I can't figure out why it won't split it by Agent first, and then by Company if it is named Direct, so I am at a road block right now.
The code I have so far is below, but for some reason, I can't figure out why it won't split it by Agent first, and then by Company if it is named Direct, so I am at a road block right now.
Sub SplitMFR()
Dim myarr As Variant
Dim vcol, i As Integer
Dim lr As Long
Dim cell As Range
Dim ws As Worksheet
Dim icol As Long
Dim Found As Range
Dim title As String
Dim titlerow As Integer
Dim wsMaster As Worksheet
Dim lastrow As Long
Dim header As Range, headers As Range
Application.ScreenUpdating = False
vcol = 17
Set ws = Sheets("Size Breaks")
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1:V1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Size Breaks"
For i = 3 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
For i = 1 To UBound(myarr)
' 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 = myarr(i) & " Size Breaks"
Else
Sheets(myarr(i) & "").Move After:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & " Size Breaks").Range("A1")
Sheets(myarr(i) & "").Columns.AutoFit
Sheets("SBD").Visible = True
Set wsMaster = ThisWorkbook.Worksheets("SBD")
wsMaster.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = myarr(i) & " Size Breaks"
Dim ghcInt As Integer, copyRng As Range, pasteRng As Range, pRow As Integer
' choose row to paste into
pRow = 1
Set headers = Worksheets(myarr(i) & " Size Breaks").Range("A1:V1")
For Each header In headers
ghcInt = GetHeaderColumnSB(header.Value)
If ghcInt > 0 Then
Set copyRng = Range(header.Offset(1), header.Cells(Rows.Count, 1).End(xlUp))
Set pasteRng = Worksheets(myarr(i) & " Size Breaks").Cells(pRow + 1, ghcInt)
copyRng.Copy
pasteRng.Offset(1).PasteSpecial xlPasteValues
End If
Next
Sheets(myarr(i) & " Size Breaks").Select
Application.DisplayAlerts = False
Sheets(myarr(i) & " Size Breaks").Delete
Application.DisplayAlerts = True
Sheets("SBD").Visible = False
Next
ws.AutoFilterMode = False
ws.Activate
Application.ScreenUpdating = True
End Sub
Function GetHeaderColumnSB(header As String) As Integer
Set headers = Worksheets(myarr(i) & " Size Breaks").Range("A1:V1")
GetHeaderColumnSB = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function