Carvalho
Carvalho

Reputation: 279

Selecting Blocks of n rows within a given range and sort them descending - VBA Excel

I´m learning the very basics of VBA coding in excel. I am trying to create a VBA code to select blocks of "n" (a given numbers of rows) within a range and then sort these rows chosen from the highest to the lowest number. Please see the example below. Any ideas?. Thank in advance.

Desired range.

enter image description here

Selected blocks

enter image description here

Desired sorting

enter image description here

Upvotes: 0

Views: 87

Answers (1)

user4039065
user4039065

Reputation:

Try working through this.

sub meh()
    dim i as long, rws as long

    rws=4

    with worksheets(1)
        for i=rws+1 to .cells(.rows.count, "A").end(xlup).row step rws *2
            with .cells(i, "A").resize(rws, 1)
                .Sort Key1:=.cells(1), Order1:=xldescending, _
                      Orientation:=xlTopToBottom, Header:=xlNo
            end with
        next i
    end with

end sub

Upvotes: 1

Related Questions