Lines with data need to become multiple lines with only 1 entry pr line for import in ERP system

Rygaard

New Member
Joined
Jan 28, 2015
Messages
11
So... i have a sheet with a lot of lines and for each line I have between 0 and 5 information's (one in its own column) i need to spread out so only 1 column.

Eksample:

Orderpart1part2par3Numbers
S001Xx24
S009x10
S011xxx15
S11510

<tbody>
</tbody>

This i need to get to this form:
Orderpartnumber
S001part124
S001Part324
S009Part210
S011part115
S011Part215
S011Part315

<tbody>
</tbody>

I think im ½ the way

First i made a Macro that filled in extra lines based on a count of how may cells in a row had X ( this i do in column P) - So for order S001 I auto generated 1 new line and took all data in other columns and pasted it down:
Code:
Sub Insert()    Dim End_Row As Long, n As Long, Ins As Long
    End_Row = Range("P" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For n = End_Row To 1 Step -1


        Ins = Cells(n, "P").Value


        If Ins > 1 Then
            Range("P" & n + 1 & ":P" & n + Ins - 1).EntireRow.Insert
        Range(Cells(n, 1), Cells(n + Ins - 1, "P")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "Q")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "R")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "S")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "T")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "U")).FillDown
        Range(Cells(n, 1), Cells(n + Ins - 1, "V")).FillDown
        ' Det her virker desvære ikke
        ' ElseIf Ins < 0 Then
        ' Range("P" & n + 1 & ":P" & n + Ins - 1).EntireRow.Delete
        
        End If


    Next n
    Application.ScreenUpdating = True
End Sub


My problem now is ... how to i make it take part 1 ... skip the blank Part 2.. and then take part 3 ?
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You may like to try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Jul48
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object, k [COLOR="Navy"]As[/COLOR] Variant, p [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant
    Ray = Cells(1).CurrentRegion
 [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, 1)) = CreateObject("Scripting.Dictionary")
            [COLOR="Navy"]End[/COLOR] If
      [COLOR="Navy"]For[/COLOR] ac = 2 To UBound(Ray, 2) - 1
        [COLOR="Navy"]If[/COLOR] UCase(Ray(n, ac)) = "X" [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 1)).exists(Ray(1, ac)) [COLOR="Navy"]Then[/COLOR]
                Dic(Ray(n, 1)).Add Ray(1, ac), Ray(n, UBound(Ray, 2))
            [COLOR="Navy"]Else[/COLOR]
                Q = Dic(Ray(n, 1)).Item(Ray(n, ac))
                Q = Q + Ray(n, UBound(Ray, 2))
                Dic(Ray(n, 1)).Item(Ray(n, ac)) = Q
            [COLOR="Navy"]End[/COLOR] If
      [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]Next[/COLOR] n
   
   
  ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 3)
  nray(1, 1) = "Order": nray(1, 2) = "Part": nray(1, 3) = "Number"
  c = 1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
       [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(k)
          c = c + 1
          nray(c, 1) = k: nray(c, 2) = p: nray(c, 3) = Dic(k).Item(p)
       [COLOR="Navy"]Next[/COLOR] p
   [COLOR="Navy"]Next[/COLOR] k
 [COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 3)
    .Value = nray
    .Borders.Weight = 2
    .Columns.AutoFit
 [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
This worked, and the more you know ... the more precise Questions you can ask - so your answer leads me to a new question - a more precise one.

what if i have the Number instead of an X
so for order S001 Under Part1 I have 2,5 and under Part3 i have 21,5

Because i just realized that some of the orders have a different Number total, than the individual parts -

if it is not possible, or to hard to code, i can do it by hand, this is still a HUGE help - But we need to enter data every week, and each ordre sheet in excel is like 400 orders long so if it is at all possible to automatize the whole thing - it would save so much time.

once more .. thank you for what you have already done !
 
Upvote 0
what if i have the Number instead of an X
so for order S001 Under Part1 I have 2,5 and under Part3 i have 21,5
Ref Quote above:- Are you saying that the results for number "S001" should be as below

ABC
1Orderpartnumber
2S001part12.5
3S001Part321.5
<colgroup><col width="27" style="width: 20pt; mso-width-source: userset; mso-width-alt: 967;"> <col width="93" style="width: 70pt; mso-width-source: userset; mso-width-alt: 3299;"> <col width="89" style="width: 67pt; mso-width-source: userset; mso-width-alt: 3157;"> <col width="105" style="width: 79pt; mso-width-source: userset; mso-width-alt: 3726;"> <tbody> </tbody>
 
Upvote 0
yes -
thank you - im sorry i did not formulate my original question correct, but i did not know what the answer would look like.

Hello MickG
Also right now i can see that i can type any headline on sheet 1, and it will just fill that in on sheet 2 - this is perfect - and way better that what i could have thought of (also that it shows results on sheet2 is Perfect! - it is clear that I did not have the experience to even formulate what I really wanted .

I have just tested the result you show... and it work for the import part

Also the "part1" or "part3" - the way it work now i can write the item number eg. Part1 = item number 8341 this work now - witch is Perfect !

(on a side note I appreciate the choice of naming of objects and variants - I wont change a thing :) )
 
Upvote 0
On the basis of "Yes" try this:-
Results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Aug59
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object, k [COLOR="Navy"]As[/COLOR] Variant, p [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant
    Ray = Cells(1).CurrentRegion
 [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, 1)) = CreateObject("Scripting.Dictionary")
            [COLOR="Navy"]End[/COLOR] If
      [COLOR="Navy"]For[/COLOR] ac = 2 To UBound(Ray, 2) - 1
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Ray(n, ac)) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 1)).exists(Ray(1, ac)) [COLOR="Navy"]Then[/COLOR]
                Dic(Ray(n, 1)).Add Ray(1, ac), Ray(n, ac)
            [COLOR="Navy"]Else[/COLOR]
                Q = Dic(Ray(n, 1)).Item(Ray(n, ac))
                Q = Q + Ray(n, ac)
                Dic(Ray(n, 1)).Item(Ray(n, ac)) = Q
            [COLOR="Navy"]End[/COLOR] If
      [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]Next[/COLOR] n
   
   
  ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 3)
  nray(1, 1) = "Order": nray(1, 2) = "Part": nray(1, 3) = "Number"
  c = 1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
       [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(k)
          c = c + 1
          nray(c, 1) = k: nray(c, 2) = p: nray(c, 3) = Dic(k).Item(p)
       [COLOR="Navy"]Next[/COLOR] p
   [COLOR="Navy"]Next[/COLOR] k
 [COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 3)
    .Value = nray
    .Borders.Weight = 2
    .Columns.AutoFit
 [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Can you use something like this? I made a small example base on your requirements. I have three formulas for your three results. I also included =iferror(...." ") to remove error messages. You need to enter with Cntrl+Shift+Enter. Then copy. Make sure you have the right cell references set up in your formulas. Assume my data sample is in A1:E5. My formula results are in A7, B7, and C7. The formula is A7 is
=IFERROR(INDEX($A$2:$A$5,SMALL(IF($B$2:$D$5="x",ROW($B$2:$D$5)-ROW($B$2)+1),ROWS($A$7:A7)))," ")
The formula in B7 is
=IFERROR(INDEX($B$1:$D$1,SMALL(IF($B$2:$D$5="x",IF($A$2:$A$5=$A7,COLUMN($B$2:$D$5)-COLUMN($B$2)+1)),COUNTIF($A7:A7,$A7)))," ")
The formula in C7 is
=INDEX($E$2:$E$5,MATCH($A7,$A$2:$A$5,0))


orderPart1Part2Part3Numbers
axx24
bx10
cxxx15
d10
aPart124
aPart124
bPart210
cPart115
cPart115
cPart115

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
MickG - That worked PERFECT - thank you so very much :)

@Mike Szczesny - thank you also, i will go with the solution MickG postede - but thank you for your time ( ill save this formula for later use )

once more.. THANK you :)
 
Upvote 0

Forum statistics

Threads
1,215,264
Messages
6,123,960
Members
449,135
Latest member
jcschafer209

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