LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default code to match and sort

Anny

tweaked code ;-)

I'm sure there are better ways but this is what I've come up with:

Sub Macro3()
' assumes heading in row 1, columns A and B
' A1 = "Div", B1 = "Level"; note that the Level heading is used later in the
sort
Dim cLevels As Collection
Dim cell As Range
Dim i As Integer

Set cLevels = New Collection
On Error Resume Next
For Each cell In Range("A1:A" & Range("A1").End(xlDown).Row)
If cell.Value = "Div" Or cell.Value = "Div1" Then
cLevels.Add Item:=cell.Offset(0, 1).Value, key:=cell.Offset(0,
1)
End If
Next cell

For i = 1 To cLevels.Count
Range("K" & i) = cLevels(i)
Next 'i

Columns("K:K").Sort _
Key1:=Range("K2"), _
Order1:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom
Range("K1:K" & Range("K1").End(xlDown).Row).Copy
Range("M1").PasteSpecial _
Paste:=xlPasteAll, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=True
Columns("K:K").Clear
Range("M1").Clear
Range("A1").Activate
End Sub

Regards

Trevor


"anny" wrote in message
...
Trevor - this code has been great for me to learn several new techniques.
It seems to have one small problem, though: it uses ALL unique values in
column B, but it should only be using the ones that are in Div1. I'd
really appreciate it if you could tweak the code.

big thanks
anny

"Trevor Shuttleworth" wrote in message
...
Anny

recording the actions and tidying up a bit (OK, a lot) gives this code:

Sub Macro2()
' assumes heading in row 1, columns A and B
Range("B1:B" & Range("B1").End(xlDown).Row).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("K1"), _
Unique:=True
Columns("K:K").Sort _
Key1:=Range("K2"), _
Order1:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom
Range("K1:K" & Range("K1").End(xlDown).Row).Copy
Range("M1").PasteSpecial _
Paste:=xlPasteAll, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=True
Columns("K:K").Clear
Range("M1").Clear
Application.CutCopyMode = False
End Sub

It assumes there is a heading in row1, columns A and B. It filters the
unique entries in column B, puts them in column K and sorts them. It
then copies them and transposes them into cell M1 and tidies up the
intermediate steps ... clears column K and cell M1.

If you have data in column E, you'll need to put the filtered Levels
somewhere else in the interim stages.

Regards

Trevor



"anny" wrote in message
...
help requested!!

Column A holds a value of Div1, Div2 or Div3
Column B holds a value from Level1 to Level6
Repeats in either column are allowed. A sample is shown below.

I need to place (in N1, O1, P1,...) the UNIQUE Levels in Division 1 (no
doubles), preferably in alpha order.
In the example, this would be Level2, Level3, Level5

A B ... N O P ...
Div1 Level5 Level2 Level3 Level5
Div1 Level2
Div2 Level5
Div3 Level2
Div1 Level5
Div1 Level3
Div2 Level6

This is part of a process that I currently do manually. I'd love to do
this by code, but I'm really stuck.

Thank you in advance for any assistance.
anny








 
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
sort & match Rae Excel Worksheet Functions 1 January 16th 07 01:30 AM
Match and sort vijaya Excel Worksheet Functions 1 November 18th 05 09:23 PM
Match and sort vijaya Excel Programming 3 November 18th 05 04:40 PM
How to match and sort lakegoddess Excel Discussion (Misc queries) 0 November 14th 05 05:14 PM
Sum if or some sort of match formula JavyD Excel Worksheet Functions 3 May 5th 05 05:14 PM


All times are GMT +1. The time now is 03:44 AM.

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"