complicated extract items from cells into multiple cells based on four or five or six items

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
hi

i need way to seperate data cell for the column into multiple cells for multiple columns based on four or five or six items . so in column E contains some items but are differnt items numbers

if I have six items lik in E2 then when split into multiple cells for multiple columns ,then the column G should be three items and the column H should be two items and the column I should be one item and the column J should be the value as in column E. and if I have five items like in E2,3,7,12,13 , then the column G should be two items and the column H should be two items and the column I should be one item and the column J should be the value as in column E. and if I have four items like E6,8,9,10,11,14, then the column G should be two items and the column H should be one item and the column I should be one item and the column J should be the value as in column E.

note: when collect some items into the cell when split the data ignore the different space . the most important should be space among the items to split for each items into multiple cells based on spaces among the items
the orginal data are in column E,F


SS .xlsm
EF
1MRGEDQTY
2BS 1200R20 18PR G580 TCF JAP100.00
3BS 1200R20 G580 TC THI200.00
4BS 1200R24 20PR G582 JAP300.00
5BS 13R22.5 R187 JAP400.00
6BS 1400R20 VSJ JAP500.00
7BS 155R12C T R623 INDO600.00
8BS 165R13C R624 INDO700.00
9BS 175/70R13 EP150 INDO800.00
10BS 175/70R14 EP150 THI200.00
11BS 1800R25** VKT JAP230.00
12BS 20.5R25** 2RR VUT IND120.00
13BS 1600-25 28PR RLS JAP100.00
14BS LT285/75R16 AT001 JAP120.00
ITEM

result from column G: J
SS .xlsm
GHIJ
1BRANDTYPEORIGINQTY
2BS 1200R20 18PRG580 TCFJAP100.00
3BS 1200R20G580 TCTHI200.00
4BS 1200R24 20PR G582JAP300.00
5BS13R22.5R187JAP400.00
6BS 1400R20VSJJAP500.00
7BS 155R12C TR623INDO600.00
8BS 165R13CR624INDO700.00
9BS 175/70R13EP150 INDO800.00
10BS 175/70R14 EP150 THI200.00
11BS 1800R25**VKTJAP230.00
12BS 20.5R25** 2RR VUTIND120.00
13BS 1600-25 28PR RLSJAP100.00
14BS LT285/75R16AT001JAP120.00
ITEM


I hope what I ask for it can be possible and logical
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Link to workbook
VBA Code:
Sub SplitItemsVarSize()

    Dim r As Long, strNorm As String
    Dim items_count As Integer, col As Integer
    Dim items_size As Variant
    Dim start_index As Variant, end_index As Variant
    Dim index As Variant
    Dim arrItems As Variant
    Dim dic As Object
    Dim arrCounts As Variant
    Dim txt As String
    Set dic = CreateObject("Scripting.Dictionary")
   
    dic(4) = Array(2, 1, 1)
    dic(5) = Array(2, 2, 1)
    dic(6) = Array(3, 2, 1)
   
    For r = 2 To 14
        strNorm = WorksheetFunction.Trim(Cells(r, "E"))
        arrItems = Split(strNorm, , , vbTextCompare)
        items_count = UBound(arrItems) + 1
        arrCounts = dic(items_count)
        end_index = -1
        col = 7 '//Column G
        For Each items_size In arrCounts
            txt = vbNullString
            start_index = end_index + 1
            end_index = end_index + items_size
            For index = start_index To end_index
                txt = txt & " " & arrItems(index)
            Next
            Cells(r, col) = Trim$(txt)
            Cells(r, "J") = strNorm
            col = col + 1
        Next
    Next
   
    MsgBox "Well done!", vbInformation
   
End Sub
 
Upvote 0
Solution
thanks, but the last column should be the numbers as in column F , your code repeat the column E into column J !
 
Upvote 0
Changed this (also have update workbook):
VBA Code:
Cells(r, "J") = strNorm
to this
VBA Code:
Cells(r, "J") = Cells(r, "F")
 
Upvote 0
1. You need to change the signature of SplitItemsVarSize() to SplitItemsVarSize(sheet As Worksheet).
2. Add procedure which loops over required sheets' name. The way you obtain the array of sheets' names is up you. In my example I'm using hard-coded inline array:
VBA Code:
Sub ProcessSeveralSheets()
    Dim sheetName As Variant
    For Each sheetName In [{"Sheet1", "Sheet2"}]
        Call SplitItemsVarSize(Sheets(sheetName))
    Next
    MsgBox "Well done!", vbInformation
End Sub
Workbook is updated.
 
Upvote 0
thanks , but it gives error as in pic in this line
VBA Code:
 Call SplitItemsVarSize(Sheets(sheetName))
1.PNG
 
Upvote 0
You need to change the signature of SplitItemsVarSize() to SplitItemsVarSize(sheet As Worksheet).
sorry ! I forgot change this as you said
many thanks ! all things are good;)
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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