Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
SteveC
 
Posts: n/a
Default Sorting alpha in two columns

Is their a way to change this:

Column A Column B
ApplesA ApplesA
ApplesB ApplesC
ApplesM Apples D
ApplesN Apples G

Into this:

Column A Column B
Apples A Apples A
Apples B
Apples C
Apples D
Apples G
ApplesM
Apples N

thanks very much!

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Sorting alpha in two columns

I saved this from a previous post.

Option Explicit
Sub testme()

Application.ScreenUpdating = False

Dim wks As Worksheet
Dim ColA As Range
Dim ColB As Range
Dim iRow As Long
Dim myCols As Long

Set wks = Worksheets("sheet1")
wks.DisplayPageBreaks = False
With wks
'row 1 has headers!
Set ColA = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
Set ColB = .Range("b2", .Cells(.Rows.Count, "B").End(xlUp))

With ColA
.Sort key1:=.Cells(1), order1:=xlAscending, header:=xlNo
End With

'change the mycols to the number of columns that
'are associated with column B

myCols = 1 ' columns B only
With ColB.Resize(, myCols)
.Sort key1:=.Cells(1), order1:=xlAscending, header:=xlNo
End With

iRow = 2
Do
If Application.CountA(.Cells(iRow, "A").Resize(1, 2)) = 0 Then
Exit Do
End If

If .Cells(iRow, "A").Value = .Cells(iRow, "B").Value _
Or Application.CountA(.Cells(iRow, "A").Resize(1, 2)) = 1 Then
'do nothing
Else
If .Cells(iRow, "A").Value .Cells(iRow, "B").Value Then
.Cells(iRow, "A").Insert shift:=xlDown
Else
.Cells(iRow, "B").Resize(1, myCols).Insert shift:=xlDown
End If
End If
iRow = iRow + 1
Loop
End With

Application.ScreenUpdating = True

End Sub


If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

SteveC wrote:

Is their a way to change this:

Column A Column B
ApplesA ApplesA
ApplesB ApplesC
ApplesM Apples D
ApplesN Apples G

Into this:

Column A Column B
Apples A Apples A
Apples B
Apples C
Apples D
Apples G
ApplesM
Apples N

thanks very much!


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
SteveC
 
Posts: n/a
Default Sorting alpha in two columns

Works great... thanks very much.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting data in columns Spiderman Excel Discussion (Misc queries) 2 October 6th 05 09:45 PM
sorting columns with more than 2 text entries J Blumenthal Excel Discussion (Misc queries) 2 September 29th 05 05:13 AM
Sorting two columns of data Max Excel Worksheet Functions 6 June 13th 05 01:30 AM
Columns are now numeric, not alpha. how to change back? samriepe Excel Discussion (Misc queries) 1 June 9th 05 03:17 PM
Sorting rows AND columns 3D Excel Worksheet Functions 2 January 7th 05 02:15 AM


All times are GMT +1. The time now is 08:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"