Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Actually i have an excel sheet something like this.
Feature 	Epic	User Story	Task/Defect
F1			
	        EP1		
		             US11	
			                    T11
			                    D11
			                    D12
		             US12	
			                    T21
			                    T22
	        EP2		
		             US21	
			                    T21
			                    D21
			                    D22
		             US22	
			                    T3
			
F2                    			
	        EP4		
		              US1	
			                    T4


But i need to write VBA code which will give output something like below
Feature 	Epic	User Story	Task/Defect
F1	         EP1	    US11	       T11
F1	         EP1	    US11	       D11
F1	         EP1	    US11	       D12
F1	         EP1	    US12	       T21
F1	         EP1	    US12	       T22
F1	         EP2	    US21	       T21
F1	         EP2	    US21	       D21
F1	         EP2	    US21	       D22
F1	         EP2	    US22	       T3
F2	         EP4	    US1	           T4



Please help me. Thanks in advance.

What I have tried:

I would like to change tree view structure to normal tabular format as decribed above.
Posted
Updated 26-Feb-18 21:50pm
v3
Comments
OriginalGriff 27-Feb-18 3:03am    
What have you tried?
Where are you stuck?
What help do you need?
[no name] 27-Feb-18 3:05am    
I am new to Macro VBA. So could you please help me. Thanks
OriginalGriff 27-Feb-18 3:17am    
Help you do what? We have no idea what you are talking about!
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And we have no idea what you have tried, and where you need help with this.
[no name] 27-Feb-18 3:45am    
i have described that i have four columns but in row my records are in Tree View format. I need to change the format as single line tabular format as i have given one sample data. Please let me know if you need more details.
Richard MacCutchan 27-Feb-18 4:11am    
You just need to start at the top cell (e.g F1), select all the empty cells below and use "Fill down". Use the macro recording feature of Excel to create the basic VBA code.

Assuming that data are stored in columns A-D and starts from row 2 (forst row = headers), check this:

VB
Option Explicit


Sub ExplodedDataToTable()
    Dim srcWsh As Worksheet
    Dim i As Long, r As Long

On Error GoTo Err_ExplodedDataToTable

    Set srcWsh = ThisWorkbook.Worksheets(1) 'you can pass the name of worksheet
    i = 2
    r = srcWsh.Range("D" & srcWsh.Rows.Count).End(xlUp).Row
    
    Do While i < r
        'remove empty row
        If srcWsh.Range("A" & i) = "" And srcWsh.Range("B" & i) = "" And _
            srcWsh.Range("C" & i) = "" And srcWsh.Range("D" & i) = "" Then
            srcWsh.Range("A" & i).EntireRow.Delete xlShiftUp
            r = r - 1
            i = i - 1
            GoTo SkipNext
        End If
        'A is not empty
        If srcWsh.Range("A" & i) <> "" And srcWsh.Range("B" & i) = "" And _
            srcWsh.Range("C" & i) = "" And srcWsh.Range("D" & i) = "" Then
            srcWsh.Range("B" & i & ":D" & i).Delete xlShiftUp
            r = r - 1
            i = i - 1
            GoTo SkipNext
        End If
        'A & B is not empty
        If srcWsh.Range("A" & i) <> "" And srcWsh.Range("B" & i) <> "" And _
            srcWsh.Range("C" & i) = "" And srcWsh.Range("D" & i) = "" Then
            srcWsh.Range("C" & i & ":D" & i).Delete xlShiftUp
            r = r - 1
            i = i - 1
            GoTo SkipNext
        End If
        'A, B & C is not empty
        If srcWsh.Range("A" & i) <> "" And srcWsh.Range("B" & i) <> "" And _
            srcWsh.Range("C" & i) <> "" And srcWsh.Range("D" & i) = "" Then
            srcWsh.Range("D" & i).Delete xlShiftUp
            r = r - 1
            i = i - 1
            GoTo SkipNext
        End If
        'A, B & C is empty, D is not empty
        If srcWsh.Range("A" & i) = "" And srcWsh.Range("B" & i) = "" And _
            srcWsh.Range("C" & i) = "" And srcWsh.Range("D" & i) <> "" Then
            srcWsh.Range("A" & i - 1 & ":C" & i - 1).Copy srcWsh.Range("A" & i)
            GoTo SkipNext
        End If
        'A, B is empty, C & D is not empty
        If srcWsh.Range("A" & i) = "" And srcWsh.Range("B" & i) = "" And _
            srcWsh.Range("C" & i) <> "" And srcWsh.Range("D" & i) <> "" Then
            srcWsh.Range("A" & i - 1 & ":B" & i - 1).Copy srcWsh.Range("A" & i)
            GoTo SkipNext
        End If
        'A, is empty, B, C & D is not empty
        If srcWsh.Range("A" & i) = "" And srcWsh.Range("B" & i) <> "" And _
            srcWsh.Range("C" & i) <> "" And srcWsh.Range("D" & i) <> "" Then
            srcWsh.Range("A" & i - 1).Copy srcWsh.Range("A" & i)
            GoTo SkipNext
        End If
        
SkipNext:
        i = i + 1
    Loop
    

Exit_ExplodedDataToTable:
    On Error Resume Next
    Set srcWsh = Nothing
    Exit Sub

Err_ExplodedDataToTable:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_ExplodedDataToTable

End Sub
 
Share this answer
 
Comments
phil.o 27-Feb-18 16:06pm    
5'd
Is there any language you're not aware of, Maciej? Impressive sum of skills, imho :)
Maciej Los 28-Feb-18 2:16am    
Yeah, there's few, such as: Assembler, Fortran... ;)

Thank you, Phil.

Cheers!
Maciej
It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.
 
Share this answer
 
Comments
[no name] 27-Feb-18 3:54am    
I have also posted so many solutions in code project but today i got unexpected answer from you. It's a 4 to 5 line of code for any experts. But if you don't know the answer then please don't reply. i don't need your help. i have asked this questions to someone who is expert in VBA code. Thanks
Dave Kreskowiak 27-Feb-18 18:17pm    
4 or 5 lines of code, says the person who can't write the code at all.

Does Solution 2 look like it's "4 or 5 lines code" to you?

I think you have some ridiculous expectations.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900