Solved

Sort Data based on date

Posted on 2011-03-14
15
183 Views
Last Modified: 2012-05-11
Hi Experts,

I would like to request Experts help create a macro to delete all prefix stars with “pcdn-tr/” at Column C and sort the data ascendant (A to Z) with condition the priority given to the nearest start date (Column H). Some header (Column C) has multiple start date at column C, for that type of data the first date become a point of reference (the multiple start date need to be maintained with the Header (Column C). Hope Experts will help me to create this feature.



FilterData.xls
0
Comment
Question by:Cartillo
  • 8
  • 7
15 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 35131963
Cartillo - if I were going to do this, I would remove the blank rows, and replicate the column C entry for each column H entry. Is that acceptable?
0
 

Author Comment

by:Cartillo
ID: 35134050
StephenJR,

Instead of copying column H data, is that possible copy column C data. E.g. at row 45 we have "Euro: Euro's Monarchies - generic :15 - Tomorrow 10/9pm". We can copy this data at row 46 to 48. We can use the same "Header" for blank row at Column C as long as the row has Start Date.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35137316
Cartillo - if I have understood correctly, try this:
Sub x()

Dim i As Long, n As Long

On Error Resume Next
Columns("H:H").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

n = Range("H" & Rows.Count).End(xlUp).Row

Range("C2").Resize(n).Replace what:="pcdn-tr/ ", replacement:=""

For i = 1 To 3
    With Range("A1:C" & n).Columns(i)
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
Next i

With Range("A1:I" & n)
    .Sort key1:=.Cells(1, 8), order1:=xlAscending, header:=xlYes
End With

End Sub

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:Cartillo
ID: 35140325
Hi StephenJR,

I need your help. After test with the actual data I realize that we need to copy data from A to G if the data at column C "Header" has multiple "Start Date". Hope you can help me to add this function.
0
 

Author Comment

by:Cartillo
ID: 35140481
Hi StephenJR,

Managed to solve above issue by modifying this line,
 
With Range("A1:C" & n).Columns(i)

but I'm not sure how to add an additional "prefix" for deletion. How to add this prefix for deletion ""pcdn-trQ/ " besides "pcdn-tr/ " at this line:

Range("C2").Resize(n).Replace what:="pcdn-tr/ ", replacement:=""

Hope you can assist

0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35140516
Cartillo,

You could add this line, before the other replace line:

Range("C2").Resize(n).Replace what:="pcdn-trQ/ ", replacement:=""
0
 

Author Comment

by:Cartillo
ID: 35140589
Hi StephenJR,

I have tried but both prefix are still exist.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35140640
Cartillo - when I tried it on the sample with the first replacement, that worked.
0
 

Author Comment

by:Cartillo
ID: 35140815
Hi StephenJR,

I made some changes at the row ( I need to reserve first few rows), dose this has any impact with the prefix deletion?  Attached the code for your perusal. I could make a blunder in this code.
Sub SortData()

Dim i As Long, n As Long

On Error Resume Next
Columns("H7:H").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

n = Range("H" & Rows.Count).End(xlUp).Row

Range("C7").Resize(n).Replace what:="pcdn-trQ/ ", replacement:=""
Range("C7").Resize(n).Replace what:="pcdn-tr/", replacement:=""



For i = 1 To 3
    With Range("A7:G" & n).Columns(i)
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
Next i

With Range("A7:I" & n)
    .Sort key1:=.Cells(1, 8), order1:=xlAscending, Header:=xlYes
End With

End Sub

Open in new window

0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35141528
What about this?
Sub SortData()

Dim i As Long, n As Long

On Error Resume Next
Range("H7", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

n = Range("H" & Rows.Count).End(xlUp).Row

Range("C7").Resize(n).Replace what:="pcdn-trQ/ ", replacement:=""
Range("C7").Resize(n).Replace what:="pcdn-tr/", replacement:=""

For i = 1 To 3
    With Range("A7:G" & n).Columns(i)
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
Next i

With Range("A7:I" & n)
    .Sort key1:=.Cells(1, 8), order1:=xlAscending, Header:=xlYes
End With

End Sub

Open in new window

0
 

Author Comment

by:Cartillo
ID: 35141744
Hi StephenJR,

Now the data are line up perfectly, but both prefix still exist.  
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35141778
Not for me. A few of these "thcn-prx/" are still there but the others disappear. Would it be easier to remove anything before (and including) "/" or could you have "/" elsewhere?
0
 

Author Comment

by:Cartillo
ID: 35141913
Hi StephenJR,

Removing remove anything before (and including) "/" would be the best option. Hope you help me to create this.
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 35142163
OK, try this:
Sub SortData()

Dim i As Long, n As Long, r As Range

On Error Resume Next
Range("H7", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

n = Range("H" & Rows.Count).End(xlUp).Row

For Each r In Range("C7").Resize(n).SpecialCells(xlCellTypeConstants)
    r = Trim(Split(r, "/")(1))
Next r

For i = 1 To 3
    With Range("A7:G" & n).Columns(i)
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
Next i

With Range("A7:I" & n)
    .Sort key1:=.Cells(1, 8), order1:=xlAscending, Header:=xlYes
End With

End Sub

Open in new window

0
 

Author Closing Comment

by:Cartillo
ID: 35142207
Cool! Thanks a lot for the great help
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question