Data Sort & paste another sheet

vidhate0123

New Member
Joined
Jun 24, 2015
Messages
18
FROMTOSIDETYPE
020LHSPQC
2030LHSDLC
3550BHSPQC
6567RHSDLC
6870RHSPQC
7177BHSDLC
8890LHSDLC
9195RHSPQC
9598BHSPQC

<tbody>
</tbody>

In above table I want to do made VBA code as requirement below.
First sort the rows Heading "TYPE" and then sort by "SIDE". after than I want to convert "BHS"rows to 2 rows Side name LHS & RHS ,other data in row remain same.
(LHS mean left hand side,RHS means Right hand Side, BHS means Both hand Side)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Seems like you'd want to do the Convert FIRST, then the Sort. Am I right, or Wrong? Please clarify for me.
 
Upvote 0
Try this on a BACK-UP COPY of your file FIRST !!!

Paste the 2 below Macros into a Standard Module. With and while your data table the Active sheet RUN ONLY the Macro named "FixMyTable". You will not need to run the 2nd Macro Seperately As it is being RUN WITHIN the "FixMyTable" Macro - Because it is BEING "CALLED" -- see the line near bottom where it reads
Call SortMyDb

Good luck. Jim

Code:
Sub FixMyTable()
Dim SR As Long, i As Long
Application.ScreenUpdating = False
SR = Range("A" & Rows.Count).End(xlUp).Row + 1
For i = SR To 2 Step -1
    If Cells(i, 3).Value = "BHS" Then
        Cells(i + 1, 3).EntireRow.Insert
            Cells(i + 1, 3).Offset(-1, 0).Rows("1:1").EntireRow.Copy
            Cells(i, 1).Offset(1).Rows("1:1").PasteSpecial
                Cells(i, 3).Value = "LHS & RHS"
                Cells(i + 1, 3).Value = "LHS & RHS"
                Application.CutCopyMode = False
    End If
Next i
Range("A1").Select
Call SortMyDb
Application.ScreenUpdating = True
End Sub


Sub SortMyDb()
Columns("A:D").Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A:D")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Range("A1").Select
End Sub
 
Last edited:
Upvote 0
Try this on a BACK-UP COPY of your file FIRST !!!

Paste the 2 below Macros into a Standard Module. With and while your data table the Active sheet RUN ONLY the Macro named "FixMyTable". You will not need to run the 2nd Macro Seperately As it is being RUN WITHIN the "FixMyTable" Macro - Because it is BEING "CALLED" -- see the line near bottom where it reads
Call SortMyDb

Good luck. Jim

Code:
Sub FixMyTable()
Dim SR As Long, i As Long
Application.ScreenUpdating = False
SR = Range("A" & Rows.Count).End(xlUp).Row + 1
For i = SR To 2 Step -1
    If Cells(i, 3).Value = "BHS" Then
        Cells(i + 1, 3).EntireRow.Insert
            Cells(i + 1, 3).Offset(-1, 0).Rows("1:1").EntireRow.Copy
            Cells(i, 1).Offset(1).Rows("1:1").PasteSpecial
                Cells(i, 3).Value = "LHS & RHS"
                Cells(i + 1, 3).Value = "LHS & RHS"
                Application.CutCopyMode = False
    End If
Next i
Range("A1").Select
Call SortMyDb
Application.ScreenUpdating = True
End Sub


Sub SortMyDb()
Columns("A:D").Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A:D")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Range("A1").Select
End Sub

Thanks For quick reply :), It work perfectly. Minor rectification was in code i.e. "LHS & RHS" replaced by LHS one time & RHS next time . That I have done. Now Sorting of LHS & RHS (SIDE Heading)still balance.
 
Upvote 0
Thanks For quick reply :), It work perfectly. Minor rectification was in code i.e. "LHS & RHS" replaced by LHS one time & RHS next time . That I have done. Now Sorting of LHS & RHS (SIDE Heading)still balance.

After making modificatuon, is the sort performing OK?
 
Upvote 0

Forum statistics

Threads
1,215,906
Messages
6,127,660
Members
449,395
Latest member
Perdi

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