+ Reply to Thread
Results 1 to 6 of 6

Add snipped to paste report in descending order

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Add snipped to paste report in descending order

    Hi,

    I have a code that extract matched rows and copy to another sheet, But it copy on next empty rows But is copying on Ascending Order (bottom of last records).

    I would like to make the change the copy/paste in descending order ( on top of last records)

    The snniped that i need to add in the code is
     Range.Sort Key1:=Range("A2"), Order1:=xlDescending
    This is the original threat:
    https://www.excelforum.com/excel-pro...e-matches.html

    and this is the original code
     Sub Treat()
    Dim RefRg As Range, R As Range
    Dim WkRg As Range, W As Range
    Dim HRg  As Range
    Dim DataRg As Range
    Const WS1Name As String = "Sheet1"
    Const WS2Name As String = "Test"
    
        Set RefRg = Sheets(WS1Name).Range("I14:M14")
        Set HRg = Sheets(WS1Name).Range("D2:P2")
        Set WkRg = Sheets(WS1Name).Range("D3:D12")
        For Each R In RefRg
            For Each W In WkRg
                If (R = W) Then
                    HRg.Copy Destination:=Sheets(WS2Name).Cells(Rows.Count, "B") '.End(3)(2)
                    W.Resize(1, HRg.Count).Copy Destination:=Sheets(WS2Name).Cells(Rows.Count, "B").End(3)(2)
                End If
            Next W
        Next R
        Application.CutCopyMode = False
        MsgBox ("Job Done")
    End Sub



    Thank you!!

    Andy

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Add snipped to paste report in descending order

    Hi Andy, played around with your code
    Public Sub Treat3()
        Dim RefRg As Range, R As Range
        Dim WkRg As Range, W As Range
        Dim HRg  As Range
        Dim DataRg As Range
        Const WS1Name As String = "Sheet1"
        Const WS2Name As String = "Test"
    
        Set RefRg = Sheets(WS1Name).Range("I14:M14")
        Set HRg = Sheets(WS1Name).Range("D2:P2")
        Set WkRg = Sheets(WS1Name).Range("D3:D12")
        Worksheets(WS2Name).Cells.Clear
        For Each R In RefRg
            For Each W In WkRg
                If (R = W) Then
                    HRg.Copy Destination:=Sheets(WS2Name).Cells(Rows.Count, "B") '.End(3)(2)
                    W.Resize(1, HRg.Count).Copy Destination:=Sheets(WS2Name).Cells(Rows.Count, "B").End(3)(2)
                End If
            Next W
        Next R
        Application.CutCopyMode = False
        Set HRg = Worksheets(WS2Name).Range("B2").CurrentRegion
        Debug.Print HRg.Address
        Worksheets(WS2Name).Sort.SortFields.Clear
        Worksheets(WS2Name).Sort.SortFields.Add Key:=Range("K" & HRg.Rows(1).Row & ":K" & HRg.Rows.Count + 1), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With Worksheets(WS2Name).Sort
            .SetRange HRg
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("B" & HRg.Rows.Count + 2).Select
        MsgBox ("Job Done")
    End Sub
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Add snipped to paste report in descending order

    Hi KebellaH,

    Sorry to bother, but The Code is replacing previous Data, also is copying the header in "Test" tab on last row 1048576
    What I need is to Copy on Top (Above of previous) Data and so on...

    I realized that the matched numbers are pasting not in the same way, so I added a column with a Date in order to sort from.

    Note: please i upload the latest sample wb with the code and the way it's displaying the output.


    Thank you So much!!

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Add snipped to paste report in descending order

    Hi Andy, will look at your file

  5. #5
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Add snipped to paste report in descending order

    Thanks Kebellah !!

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Add snipped to paste report in descending order

    Hope Treat 4 does the trick.
    The target sheet is the worksheet named Keebellah check it before running the macro so you see what it is now, then got back to the data sheet and press Treat 4
    Bed-time here
    Good night

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2007 : Extract by descending order
    By Shilpa kanchan in forum Excel General
    Replies: 1
    Last Post: 12-25-2011, 03:53 AM
  2. Macro for arranging in descending order.
    By Taureankv in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2010, 10:24 PM
  3. vlookup in descending order
    By thebigmancometh in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-01-2010, 09:41 AM
  4. vlookup in descending order
    By thebigmancometh in forum Excel General
    Replies: 1
    Last Post: 02-16-2010, 06:13 AM
  5. Line graph in descending order
    By jsmity in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 12-08-2009, 04:38 PM
  6. descending order using paste special
    By devesh.agrawal in forum Excel General
    Replies: 2
    Last Post: 05-08-2009, 12:46 AM
  7. Sort in descending order
    By shahcu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2008, 03:01 AM
  8. autofill in descending order
    By suzzmenn in forum Excel General
    Replies: 1
    Last Post: 09-15-2007, 01:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1