Click here to Skip to main content
16,015,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have my data as below.

input:

Loc_id name
0 hyd
HYD001 hyd
0 hyd
0 hyd
Abc.a ban
0 ban
0 ban
Ram pune
0 xyz
0 ban
Loc_id name
0 hyd
HYD002 hyd
0 hyd
0 hyd
Abc.a ban
0 ban
0 ban
Ram pune
0 xyz
0 ban

I need to see data as below format

C#
Required Op:
	
loc_id	name
HYD001	hyd
HYD001	hyd
HYD001	hyd
HYD001	hyd
HYD001	ban
HYD001	ban
HYD001	ban
HYD001	pune
HYD001	xyz
HYD001	ban
HYD002	hyd
HYD002	hyd
HYD002	hyd
HYD002	hyd
HYD002	ban
HYD002	ban
HYD002	ban
HYD002	pune
HYD002	xyz
HYD002	ban


Thanks in Advance.

What I have tried:

I have tried with below code.But this code is working statically.But my requirement is to change data Dynamically

Sub REPLACE()

Worksheets("Sheet1").Columns("A").REPLACE _
What:="Abc.a", Replacement:="HYD001", _
SearchOrder:=xlByColumns, MatchCase:=True

End Sub
Posted
Updated 12-Sep-16 2:31am
v2

1 solution

Check this: http://www.codeproject.com/Questions/1126932/How-to-delete-sub-header-columns-in-excel-VBA#answer2[^]

Based on above you were able to change the code to your needs:

VB
Sub Cleaning3()
    Dim wsh As Worksheet
    Dim i As Integer ', idcounter As Integer
    'context
    Set wsh = ThisWorkbook.Worksheets(1)
    '
    'idcounter = 1
    'starting row
    i = 2
    'till cell in col. A is not empty
    Do While wsh.Range("A" & i) <> ""
        'if zero - get value from below cell
        If wsh.Range("A" & i) = 0 Then
            wsh.Range("A" & i) = wsh.Range("A" & i).Offset(RowOffset:=1)
        End If
        'if cell contains non-numeric value and it's not a "id" - get value from above cell
        If LCase(wsh.Range("A" & i)) <> "loc_id" And Not wsh.Range("A" & i) Like UCase(wsh.Range("B2")) & "*" Then
            wsh.Range("A" & i) = wsh.Range("A" & i).Offset(RowOffset:=-1)
        End If
        'if "id" - remove entire row
        If LCase(wsh.Range("A" & i)) = "loc_id" Then
            wsh.Range("A" & i).EntireRow.Delete xlShiftUp
            i = i - 1
        End If
        i = i + 1
    Loop
 
End Sub
 
Share this answer
 
v2
Comments
Member 12726897 12-Sep-16 8:50am    
Thanks Maciej Los,

After executing above code I am getting output as below.

id name
0 hyd
0 q
0 ban
0 ban
0 ban
0 pune
0 hyd
0 hyd
0 ban
0 ban
0 ban
0 pune
0 xyz

Could you Please help me.
Maciej Los 12-Sep-16 8:55am    
You're very welcome. Have you tried to change to code to your needs? I bet - you don't!
Member 12726897 13-Sep-16 1:30am    
Hi,

Actually I am SQL Developer(Reporting Sap BO,Tableau) So I need to generate a report based on transposed(converted) data. I am very new to VBA i.e's why I asked you again and again. Any way thanks for your help & I will try on my own ,I will be in touch with you if I need any assistance.
Maciej Los 13-Sep-16 2:08am    
You're very welcome.

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