Separate the lines based on column header and its value

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi Team,

Hope anyone can help me, I have column heading fields like New, Old, Exist, Duplicate and each column has its own values and require vba code to get each line separately. For example for product A, if you have value in New and Duplicate amounting $1000 and $2000 respectively then it shows in single line in both columns but I should be two different columns saying one for New and other for Duplicate.

Example:
Input data
Product Name
NewOldExistsDuplicate
Product 1
$ 1,000 $ - $ 100 $ 10
Product 2 $ 2,000 $ 20 $ - $ -
Product 3 $ 300 $ 500 $ 1,000 $ 100
Product 4 $ 1,000 $ 250 $ 350 $ 500

<colgroup><col><col span="4"></colgroup><tbody>
</tbody>


Output:

Product Name
TypeAmount
Product 1New $ 1,000
Product 1Exists $ 100
Product 1Duplicate $ 10
Product 2New $ 2,000
Product 2
Old $ 20
Product 3
New $ 300
Product 3Old $ 500
Product 3
Exists $ 1,000
Product 3
Duplicate $ 100
Product 4
New $ 1,000
Product 4Old
$ 250
Product 4Exists $ 350
Product 4Duplicate $ 500

<colgroup><col><col span="2"></colgroup><tbody>
</tbody>


Thank you,
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Sep35
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Ray = ActiveSheet.Cells(1).CurrentRegion
ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 3)
nray(1, 1) = "Product Name": nray(1, 2) = "Type": nray(1, 3) = "Amount"
c = 1
[COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
    [COLOR="Navy"]For[/COLOR] Ac = 2 To 5
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            nray(c, 1) = Ray(n, 1)
            nray(c, 2) = Ray(1, Ac)
            nray(c, 3) = Ray(n, Ac)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] n
[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
Super!! Really appreciate your efforts ....But if I want to customize the columns ..I mean to say ...if I want to increase the column range...where should I change in the code....it should copy the entire row and create separate and I have around 100 columns...will it works ?

Actual data format :
I have columns "A" to "FK" and condition should check in column "P","R","T","V","X","Z","AB","AD" and create the data as per the these columns ....
 
Last edited:
Upvote 0
If you saying the data could have up to 100 columns , but the result will still only have 3, then you should just need to change the line below:-

From this:-
Code:
[COLOR=#000080]For[/COLOR] Ac = 2 To 5

To:-
Code:
[COLOR=#000080]For[/COLOR] Ac = 2 To ubound(ray,2)
 
Upvote 0
what should I change here....

With Sheets("Sheet2").Range("A1").Resize(c, 1)

incase change in the columns, I'm getting debug here....
 
Upvote 0
That line on the original code was "Resize(c,3)" not "Resize(c,1)"
I increased my columns to 125, and the code still works, with the previous mod.
I Imagine your Results data should still be in 3 columns, so that should not change.
If its still not working , post a basic example of data that's not working.
 
Upvote 0
Hope anyone can help me, I have column heading fields like New, Old, Exist, Duplicate and each column has its own values and require vba code to get each line separately. For example for product A, if you have value in New and Duplicate amounting $1000 and $2000 respectively then it shows in single line in both columns but I should be two different columns saying one for New and other for Duplicate.

Example:
Input data
Product Name
NewOldExistsDuplicate
Product 1 $ 1,000 $ - $ 100 $ 10
Product 2 $ 2,000 $ 20 $ - $ -
Product 3 $ 300 $ 500 $ 1,000 $ 100
Product 4 $ 1,000 $ 250 $ 350 $ 500

<tbody>
</tbody>
Are the values displayed in the cells the results of Cell Formatting? In other words, if I selected Product 1 in the New column, what is displayed in the Formula Bar... 1000? What about Product 1 in the Old column... is it 0 or blank?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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