RedOctoberKnight
Board Regular
- Joined
- Nov 16, 2015
- Messages
- 153
- Office Version
- 2016
- Platform
- Windows
Good Afternoon,
I have the following code that I found online (not taking credit for it at all as it's way past my skill level). As most of you can probably tell by reading it, it takes a range of data and sorts it into different sheets by whichever column you choose. The code works great but i'm hoping to eliminate some steps. It required you to select the column headers and then requires you to select all the data in the column from which you want to sort.
My header columns are always going to be A16:N16 and my sorting column will always start at K17. I want to have the sorting column find the last row with data and use it as the range. So basically K17:K??. I basically want to eliminate having to input the sorting parameters myself.
There are a few "notes" that I have tried to use but it doesn't work. Any help would be much appreciated.
I have the following code that I found online (not taking credit for it at all as it's way past my skill level). As most of you can probably tell by reading it, it takes a range of data and sorts it into different sheets by whichever column you choose. The code works great but i'm hoping to eliminate some steps. It required you to select the column headers and then requires you to select all the data in the column from which you want to sort.
My header columns are always going to be A16:N16 and my sorting column will always start at K17. I want to have the sorting column find the last row with data and use it as the range. So basically K17:K??. I basically want to eliminate having to input the sorting parameters myself.
There are a few "notes" that I have tried to use but it doesn't work. Any help would be much appreciated.
VBA Code:
Sub Splitdatabycol()
'by Extendoffice
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
Dim xTRg As Range
Dim xVRg As Range
Dim xWSTRg As Worksheet
On Error Resume Next
'xTRg = Range("A16:N16")
Set xTRg = Application.InputBox("HIGHLIGHT THE COLUMN HEADERS IN BLUE:", "Kutools for Excel", "", Type:=8)
If TypeName(xTRg) = "Nothing" Then Exit Sub
Set xVRg = Application.InputBox("HIGHLIGHT THE DAYS OF THE WEEK COLUMN IN GREEN:", "Kutools for Excel", "", Type:=8)
If TypeName(xVRg) = "Nothing" Then Exit Sub
'Set xVRg = Range("K17").End(xlDown).Row
vcol = xVRg.Column
Set ws = xTRg.Worksheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = xTRg.AddressLocal
titlerow = xTRg.Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
Application.DisplayAlerts = False
If Not Evaluate("=ISREF('xTRgWs_Sheet!A1')") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
Else
Sheets("xTRgWs_Sheet").Delete
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
End If
Set xWSTRg = Sheets("xTRgWs_Sheet")
xTRg.Copy
xWSTRg.Paste Destination:=xWSTRg.Range("A1")
ws.Activate
For i = (titlerow + xTRg.Rows.Count) 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 = 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) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
End If
xWSTRg.Range(title).Copy
Sheets(myarr(i) & "").Paste Destination:=Sheets(myarr(i) & "").Range("A1")
ws.Range("A" & (titlerow + xTRg.Rows.Count) & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A" & (titlerow + xTRg.Rows.Count))
Sheets(myarr(i) & "").Columns.AutoFit