Code change required

bajrang2101

New Member
Joined
Dec 4, 2014
Messages
21
Dear All,

I have the following code which is working fine when I am using the columns as under:

Module Step #
Comm Step 1
Comm Step 2
PD Step 3
Comm Step 4
Comm Step 5
Broner Step 6
Broner Step 7
Broner Step 8

Sub alternative()
Dim lr As Long, c(), a, i As Long, k As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
ReDim c(1 To lr, 1 To 2)
With Range("A1").Resize(lr, 2)
a = .Value
c(1, 1) = a(1, 1): c(1, 2) = a(1, 2): k = 1
For i = 2 To lr
If a(i, 1) <> a(i - 1, 1) Then
k = k + 1
c(k, 1) = a(i, 1)
c(k, 2) = a(i, 2)
Else
c(k, 2) = c(k, 2) & vbLf & a(i, 2)
End If
Next i
.ClearContents
.Resize(k, 2) = c
End With
End Sub

But I want to use this code when the same columns are reversed as mentioned and it should give the same result as earlier.

Step # Module
Step 1 Comm
Step 2 Comm
Step 3 PD
Step 4 Comm
Step 5 Comm
Step 6 Broner
Step 7 Broner
Step 8 Broner

Can anybody please help me modify the following code to give the desired result?

Thanks in advance..!

Best Regards,
Bajrang
 

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.
Hi

Try :-
Code:
Sub alternative()
Dim lr As Long, c(), a, i As Long, k As Long, x, y
 
lr = Range("A" & Rows.Count).End(xlUp).Row
ReDim c(1 To lr, 1 To 2)
x = Application.WorksheetFunction.Match("Module", Range("A1:B1"), 0)
y = IIf(x = 1, 2, 1)
With Range("A1").Resize(lr, 2)
    a = .Value
    c(1, x) = a(1, x): c(1, y) = a(1, y): k = 1
    For i = 2 To lr
        If a(i, x) <> a(i - 1, x) Then
            k = k + 1
            c(k, x) = a(i, x)
            c(k, y) = a(i, y)
        Else
            c(k, y) = c(k, y) & vbLf & a(i, y)
        End If
    Next i
    .ClearContents
    .Resize(k, 2) = c
End With
'
End Sub

hth
 
Upvote 0
Thanks so much Mike. This works.

Can this be extended for multiple scripts in a sheet as explained with Before and After requirement?

Before:

[TABLE="width: 444"]
<colgroup><col width="148" span="3" style="width:111pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 148"]Script #[/TD]
[TD="class: xl63, width: 148"]Step #[/TD]
[TD="class: xl63, width: 148"]Module[/TD]
[/TR]
[TR]
[TD="class: xl63"]Script 1[/TD]
[TD="class: xl64, width: 148"]Step 1[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 2[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl63"]Step 3[/TD]
[TD="class: xl63"]PD[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl63"]Step 4[/TD]
[TD="class: xl63"]PD[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl63"]Step 5[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 6[/TD]
[TD="class: xl63"]Broner[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl63"]Step 7[/TD]
[TD="class: xl63"]Broner[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl63"]Step 8[/TD]
[TD="class: xl63"]Broner[/TD]
[/TR]
[TR]
[TD="class: xl63"]Script 2[/TD]
[TD="class: xl64, width: 148"]Step 1[/TD]
[TD="class: xl63"]Finance[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 2[/TD]
[TD="class: xl63"]Finance[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 3[/TD]
[TD="class: xl63"]Finance[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 4[/TD]
[TD="class: xl63"]PD[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 5[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 6[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 7[/TD]
[TD="class: xl63"] [/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 8[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 9[/TD]
[TD="class: xl63"]Comm[/TD]
[/TR]
[TR]
[TD="class: xl63"] [/TD]
[TD="class: xl64, width: 148"]Step 10[/TD]
[TD="class: xl63"]PD[/TD]
[/TR]
</tbody>[/TABLE]

After:
[TABLE="width: 258"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Script #[/TD]
[TD]Step #[/TD]
[TD]Module[/TD]
[/TR]
[TR]
[TD]Script 1[/TD]
[TD]Step 1
Step 2[/TD]
[TD]Comm[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 3
Step 4[/TD]
[TD]PD[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 5[/TD]
[TD]Comm[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 6
Step 7[/TD]
[TD]Broner[/TD]
[/TR]
[TR]
[TD]Script 2[/TD]
[TD]Step 1
Step 2
Step 3[/TD]
[TD]Finance[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 4[/TD]
[TD]PD[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 5
Step 6[/TD]
[TD]Comm[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 7[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 8
Step 9[/TD]
[TD]Comm[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]Step 10[/TD]
[TD]PD[/TD]
[/TR]
</tbody>[/TABLE]
Regards,
Bajrang
 
Upvote 0
The cross posting has been updated as Solved.

Please provide me with the solution for this post as required.

Best Regards,
Bajrang
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,958
Members
452,158
Latest member
MattyM

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