Problem sorting a range to single column

54edser

New Member
Joined
Sep 3, 2014
Messages
4
Hi All, I have a macro that sorts cells in a range (A2:H10) with data (Names) into a single column in A.


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


Sub SortRange()
Dim rRange As Range
Dim lCol As Long

Set rRange = Range("A2:H10")


With rRange
For lCol = 1 To .Columns.Count
With .Columns(lCol)
.Copy
Range("A2:H10").End(xlDown)(2, 1).PasteSpecial xlValues
Application.CutCopyMode = False
End With
Next lCol
End With

End Sub


I have problems, firstly when sorting, the macro pastes from the next vacant cell in row A so I end up with the original data in column A duplicated, Secondly if there are any gaps in the data e.g. D2 has no content then none of the cells in Column D will be sorted. Is it possible to modify the macro to a). ignore gaps and sort all the cells in the range, and b). eliminate duplicates in Column A or move sorted range to another Column. Thanks in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

RickXL

MrExcel MVP
Joined
Sep 9, 2013
Messages
4,328
Hi and welcome to the forum.

This should be an improvement:
Code:
Sub SortRange()
     Dim rRange As Range
     Dim lCol As Long
    
     Set rRange = Range("A2:H10")
    
     With rRange
        For lCol = 2 To .Columns.Count
            With .Columns(lCol)
                .Copy
                Cells(Rows.Count, "A").End(xlUp)(2, 1).PasteSpecial xlValues
                Application.CutCopyMode = False
            End With
        Next lCol
     End With

 End Sub

Start the copy from column B (ICol=2).
Find the last empty cell starting from the bottom.
Code:
   Cells(Rows.Count, "A").End(xlUp)(2, 1).PasteSpecial xlValues

I hope this helps,
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,826
Messages
5,855,873
Members
431,771
Latest member
CoryMelth

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
Top