Here is updated code with 2 changes: 1) paste the 6th category (in my case), and 2) paste to each new sheet starting at A24, not A1:
Code:
Sub cat()
Dim wb As Workbook
Dim wsMain As Worksheet
Dim wsTarget As Worksheet
Dim rngMain As Range
Dim rngCopy As Range
Dim lastRow As Long
Dim catNo 'category number
Dim i As Long
Dim currCat As String 'current category
Dim rowStart As Long
Dim rowEnd As Long
Dim shtNew As String
Dim shtCount As Integer
Set wb = Workbooks("CopyDataByCategory.xlsm")
Set wsMain = wb.Worksheets("main")
lastRow = wsMain.Range("g1").End(xlDown).Row
Set rngMain = wsMain.Range("a1", "g" & lastRow)
wsMain.Sort.SortFields.Clear
wsMain.Sort.SortFields.Add Key:=Range("g2", "g" & lastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With wsMain.Sort
.SetRange Range("a1", "g" & lastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
rowStart = 2
currCat = wsMain.Range("g" & rowStart).Value
For i = 2 To lastRow + 1 '*************** added the "+ 1" here ******************
If i > rowStart Then
If wsMain.Range("g" & i).Value <> wsMain.Range("g" & rowStart).Value Then
'found the next category
rowEnd = i - 1
shtNew = "JV" & currCat
shtCount = wb.Worksheets.Count
Set wsTarget = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
wb.Worksheets(shtCount + 1).Name = shtNew
wsMain.Range("a" & rowStart, "f" & rowEnd).Copy
wsTarget.Range("a24").PasteSpecial 'pastes to A24 now *******************************
rowStart = rowEnd + 1
currCat = wsMain.Range("g" & rowStart).Value
i = i - 1
Application.CutCopyMode = False
End If
End If
Next i
wsMain.Select
End Sub
My code is contingent on having the "category value" in column G, but I don't see values in that column on your template, unless I'm missing it?