Thread: Macro Help
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Macro Help

I think this is easier than your method. I created an auxilary column with
the unique values. Then simply did a look up to find which items existed and
which didn't.


Sub matchrows()
'create new column C with unique values sorted
'create new column C, copy old c (now D) to c
Columns("C:C").Insert
Columns("D:D").Copy Destination:=Columns("C:C")

EndC = Range("C" & Rows.Count).End(xlUp).Row
EndE = Range("E" & Rows.Count).End(xlUp).Row
Range("E1:E" & EndE).Copy Destination:=Range("C" & (EndC + 1))

'insert new column to put unique values
Columns("C:C").Insert
'remove duplicates
EndD = Range("D" & Rows.Count).End(xlUp).Row
Range("D1:D" & EndD).AdvancedFilter _
Action:=xlFilterCopy, _
Unique:=True, _
CopyToRange:=Range("C1")
'delete original data
Columns("D").Delete

'sort column descending sending blank cells to end
EndC = Range("C" & Rows.Count).End(xlUp).Row
Range("C1:C" & EndC).Sort _
key1:=Range("C1"), _
Order1:=xlDescending, _
header:=xlNo

'sort back into correct order
EndC = Range("C" & Rows.Count).End(xlUp).Row
Range("C1:C" & EndC).Sort _
key1:=Range("C1"), _
Order1:=xlAscending, _
header:=xlNo

'create new rows D & E and move items to F to G matching unique items
Columns("D").Insert
Columns("D").Insert

'lookup items in E using column C and move to D
Range("D1").Formula = "=IF(IsNA(Match($C1,F$1:F$" & EndC & ",0)),"""",$C1)"

'copy formual to entire area
Range("D1").Copy Destination:=Range("D1:E" & EndC)

'replace formula with values
Range("D1:E" & EndC).Copy
Range("D1:E" & EndC).PasteSpecial _
Paste:=xlPasteValues

'delete extra rows
Columns("F:G").Delete
Columns("C").Delete

End Sub


"Stanley Braverman" wrote:

I have column C and column D that contains data. I need macro that will IF
THEN with a loop till done;

1st condition. If C1 = D1 then do nothing(but look at next cells C2 and D2
etc. Etc.
2nd condition. If C1 < D1 then move cells in column C down.
(one cell at a time until condition one meet again and continue loop)
3rd condition. If C1 D1 then move cells in column D down.
(one cell at a time until condition one meet again and continue loop)

Perhaps instead of a loop maybe an input for how many rows to do

By doing this cells that are being moved will leave a bank cell in it's
place and that is what is wanted.
Example:
PS1495-15 PS1495-15
PS1495-16 PS1495-16
PS1495-17
PS1495-18 PS1495-18
PS1495-19 PS1495-19

PSJT304-16 PSJT304-16
PSJT304-17 PSJT304-17
PSJT304-18 PSJT304-18
PSJT305-15
PSJT314-01 PSJT314-01
PSJT314-02 PSJT314-02


Thanks