split data from sheet into multiple sheets based on first two letters

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
hi
I would split data from sheet tires into multiple sheets based on first for two letters (FS,BS.....) and when split data should arrange data the numbers from small to big.
so as you see in sheet BS when arrange should search the less number after R and arrange to big and should search the less number in the beginning item before letter R and arrange to big.
and should replace data in divided sheet when change or add new data in sheet TIRES.
original data in sheet TIRES
list1.xlsx
ABC
1ITEMBRANDQTY
21BS 175/70R13 B25 INDO230
32BS 175/70R13 EP150 THI123
43BS 195/60R15 AR20 INDO2300
54BS 195/60R15 EP150 THI500
65BS 195/60R15 T001 JAP1535
76BS 195/60R15 150EZ THI2570
87BS 195/65R15 MY02 THI3605
98BS 205/70R15 694 JAP4640
109BS 185/70R13 EP150 INDO5675
1110BS 175/65R14 EP150 THI6710
1211BS 175/65R14 B25 INDO7745
1312BS 175/70R14 EP150 THI8780
1413BS 175/70R14 MY02 THI9815
1514BS 185/65R14 TEC THI10850
1615BS 185/65R14 150EZ INDO11885
1716BS 185/65R14 EP150 INDO12920
1817BS 195/70R14 150EZ INDO13955
1918BS 185/65R15 TC10 INDO14990
2019BS 185/65R15 T005 INDO16025
2120BS 185/65R15 T01 JAP17060
2221BS 185/65R15 B250 JAP18095
2322FS 205/55R16 RE003 THI24305
2423FS 205/65R15 EP150 INDO25340
2524FS 205/65R15 MY02 THI26375
2625FS 195/65R15 EP150 THI27410
2726FS 195/65R15 EP150 JAP28445
2827FS 195/65R15 T001 JAP29480
2928FS 195/55R16 EP300 THI30515
3029FS 205/65R15 TZ700 JAP31550
TIRES


result after split for each sheet
list1.xlsx
ABC
1ITEMBRANDQTY
21FS 195/65R15 EP150 THI27410
32FS 195/65R15 EP150 JAP28445
43FS 195/65R15 T001 JAP29480
54FS 205/65R15 EP150 INDO25340
65FS 205/65R15 MY02 THI26375
76FS 205/65R15 TZ700 JAP31550
87FS 195/55R16 EP300 THI30515
98FS 205/55R16 RE003 THI24305
FS


list1.xlsx
ABC
1ITEMBRANDQTY
21BS 175/70R13 B25 INDO230
32BS 175/70R13 EP150 THI123
43BS 185/70R13 EP150 INDO5675
54BS 175/65R14 EP150 THI6710
65BS 175/65R14 B25 INDO7745
76BS 175/70R14 EP150 THI8780
87BS 175/70R14 MY02 THI9815
98BS 185/65R14 TEC THI10850
109BS 185/65R14 150EZ INDO11885
1110BS 185/65R14 EP150 INDO12920
1211BS 195/70R14 150EZ INDO13955
1312BS 185/65R15 TC10 INDO14990
1413BS 185/65R15 T005 INDO16025
1514BS 185/65R15 T01 JAP17060
1615BS 185/65R15 B250 JAP18095
1716BS 195/60R15 AR20 INDO2300
1817BS 195/60R15 EP150 THI500
1918BS 195/60R15 T001 JAP1535
2019BS 195/60R15 150EZ THI2570
2120BS 195/65R15 MY02 THI3605
2221BS 205/70R15 694 JAP4640
BS
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
paste code into a module then run ParseData()
tho for some reason the sort isn't working, so I commented it out: SortData

Code:
Option Explicit

Public Sub ParseData()
Dim sName As String
Dim wsSrc As Worksheet, wsTarg As Worksheet
Dim colNames As New Collection
Dim iRows As Long, iRows2 As Long
Dim n As Integer
On Error GoTo errPars
Set wsSrc = ActiveSheet
Range("d1").Value = "sort"
Range("b2").Select
iRows = ActiveSheet.UsedRange.Rows.Count
'collect sort values
While ActiveCell.Value <> ""
      sName = Left(ActiveCell.Value, 2)
      ActiveCell.Offset(0, 2).Value = sName
      If sName <> "" Then colNames.Add sName, sName
   
      ActiveCell.Offset(1, 0).Select  'next row
Wend

'=copy data to its own sheet
For n = 1 To colNames.Count
   Range("A1").Select
    Selection.AutoFilter   'filter on
    ActiveSheet.Range("$A$1:$D$" & iRows).AutoFilter Field:=4, Criteria1:=colNames(n)
    Range("A1:D" & iRows).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    ActiveSheet.Name = colNames(n)
    Application.CutCopyMode = False

   Range("A1").Select
   iRows2 = ActiveSheet.UsedRange.Rows.Count
   
      'error here for some reason during sort
   'sortData iRows2
   
   FitCols
   wsSrc.Activate
   Selection.AutoFilter  'filter off
   
Next
  'show results
'wsTarg.Activate
MsgBox "Done"
Exit Sub
errPars:
If Err = 457 Then
   Resume Next
Else
MsgBox Err.Description, , Err
End If
Exit Sub
Resume
End Sub
Sub sortData(ByVal pvRows)
Dim ws As Worksheet
'---sort data for export
Set ws = ActiveSheet
    Range("b2").Select
    ws.Sort.SortFields.Clear
    ws.Sort.SortFields.Add Key:=Range("B2:D" & pvRows), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ws.Sort.SortFields.Add Key:=Range("C2:C" & pvRows), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ws.Sort
        .SetRange Range("A1:D" & pvRows)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub FitCols()
    Columns("A:A").ColumnWidth = 4.29
    Columns("B:B").ColumnWidth = 40.29
    Columns("C:C").ColumnWidth = 10.43
    Columns("D:D").ColumnWidth = 4.29
    Cells.Select
    Selection.RowHeight = 16.5
    Range("A1").Select
End Sub
 
Upvote 0
thanks and I appreciate your help, but unfortunately there are two problems
first doesn't arrange as in OP into sheet FS. this is what I got based on your code
w.xlsm
ABC
1ITEMBRANDQTY
222FS 205/55R16 RE003 THI24305
323FS 205/65R15 EP150 INDO25340
424FS 205/65R15 MY02 THI26375
525FS 195/65R15 EP150 THI27410
626FS 195/65R15 EP150 JAP28445
727FS 195/65R15 T001 JAP29480
828FS 195/55R16 EP300 THI30515
929FS 205/65R15 TZ700 JAP31550
FS

second if I run macro repeatedly it gives error . it should update when I add new data or change in sheet TIRES when every time run the macro .
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: split data from sheet into multiple sheets based on first two letters
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top