Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Sort current usded range

  1. #1

    Sort current usded range

    Hi

    I am in need of hopefully a small piece of code to sort the current used range which does not have a header and the column to sort on is whichever column the cursor is currently in.

    Does that make sense?

    If for example the the cursor is in G45 and the current used range extends from E12 to I52 I would like that range sorted by column G.

    And as does happen frequently the cursor is in E52 then the same range whould be sorted on column E.

    Am I asking too much?

    If this can be achieved it would save me hours during the year.

    My regards from a rather sunny English day

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.
    try this

    Sub Sort_Activecell_CurrentRegion()
        ActiveCell.CurrentRegion.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlNo
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3

    Sort current used range

    mancubus

    Thank you, it's wonderful and will save me hours .
    And in just one line of code.

    Again my sincere thanks for taking the time.
    Last edited by danesrood; 03-11-2015 at 05:49 AM. Reason: typo

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    i assume the code will be run when the worksheet which houses the range to be sorted is the activesheet.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Yes

    I spend a number of hours each week working on data that comes in to me where I need to re-sort the data in various ways so believe me this piece of code will work wonders for me.

    In fact I have used it quite a few times this afternoon.

    Again my thanks

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i am glad it helped.

    please mark the thread as solved from thread tools dropdown (above the first message) for future references.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Hi

    Good morning from what will soon be a nice sunny English day – fingers crossed.

    I wonder if it is possible to add a bit more functionality to the excellent line of code that you produced for me recently.

    ActiveCell.CurrentRegion.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlYes

    Some years ago somebody very kindly gave me the following code which basically sets the row height to best fit and then gives me the option to set the height to a specific size that I type in.

    This code specifies the range to work on but I would like it amended slightly to work on the same region that has just been sorted by your code.

    I hope that this makes sense

    Sub RowHeightChange()
    Dim LstRow As Long
    Dim RwHt As Double
    LstRow = Application.Range("C65536").End(xlUp).Row
    Range("C5:H" & LstRow).Select
    Cells.EntireRow.AutoFit
    RwHt = Application.InputBox("Please enter the height that you want", Type:=2)
    For i = 1 To LstRow
    If Range("A" & i).EntireRow.RowHeight <= RwHt Then
    Range("A" & i).RowHeight = RwHt
    End If
    Next
    End Sub

    Thank you

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    please use code tags when posting your code.
    # button will do it automatically. click the button code tags will be inserted. paste your code in between these tags:

    [ CODE ]your code here[ /CODE ]



    assuming your range's TopLeftCell is not A1, for ex C5:H49, perhaps:

    Sub RangeSortAndRowHeightChange()
    
        Dim RwHt As Double, i As Long
        Dim rng As Range
        
        Set rng = ActiveCell.CurrentRegion
        
        rng.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlYes
        rng.Rows.AutoFit
        
        RwHt = Application.InputBox("Please enter the height that you want", Type:=2)
        
        For i = (rng.Rows(1).Row) To (rng.Rows(1).Row + rng.Rows.Count - 1)
            If Cells(i, rng.Columns(1).Column).RowHeight < RwHt Then 'no need for "=" sign
                Cells(i, rng.Columns(1).Column).RowHeight = RwHt
            End If
        Next
    
    End Sub

    rng.Rows(1).Row = 5
    rng.Rows.Count = 45
    rng.Rows(1).Row + rng.Rows.Count - 1 = 49

    Columns(1).Column = 3, which is C.

    so For Next will loop from C5 to C49
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    mancubus

    Works a treat thank you so much.

    Can I just ask, is it possible to add an extra value to increase Best Fit by say 2 or 3 or is that just not possible by the very nature of BestFit.

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    you mean?
    RwHt = Application.InputBox("Please enter the height that you want", Type:=2) + 2
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    No, adding an additional amount of space to BestFit

  12. #12
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    the code first autofits rows, then changes these widths to user input rowheight, if they are smaller.

    now you want to increase the unchanged widths by 2 points?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  13. #13
    When you put it like that it sounds dopey.

    What I find is that BestFit is a bit tight for my eyes or liking so yes I have a bash at setting a size that I think will do for all 1 line rows but there are sometimes rows that consist of 2 or 3 lines of data.

    Don't get me wrong what I have got now is great and I am very pleased with, I was just wondering as a step towards my perfection if it was conceivable to add a bit extra to the space created by BestFit.

  14. #14
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    no. it's about vba properties and methods you may or may not familiar with. i'm having difficulty understanding your requirement.

    the best way to read a multi line text in a cell (or range) without changing the column width is to set the WrapText property of that cell to True.

    perhaps:
    Sub RangeSortAndRowHeightChange()
         
        Dim i As Long
        Dim rng As Range
         
        Set rng = ActiveCell.CurrentRegion
         
        rng.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlYes
        rng.WrapText = True
         
        For i = (rng.Rows(1).Row) To (rng.Rows(1).Row + rng.Rows.Count - 1)
            With Cells(i, rng.Columns(1).Column)
                .RowHeight = .RowHeight + 2
            End With
        Next
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  15. #15
    Thank you for the time you have spent on this and I apologise if I am digging myself into a hole so to speak.
    Your lates bit of code doesn't seem to work as in sorting the current range backwards and forwards it is increasing the height of the rows by 2 each time and in some situations the two or three line cells aren't visible.

    I'm very happy to stick with your earlier code which joins your sort option which is great and the original row height code.

    However if you do want to go further what I think I would like to achieve in essence is that the row height for each row is set to bestfit each time and then an extra value is added to make it easier to read and print.

  16. #16
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    if the below code won't work for you, please post a sample workbook which will demonsrate both 'before' and 'after' (macro) structure of your table in two separate worksheets.

    other members can post to their solutions to the thread as well.

    Choose the the method for determining the additional heigth. either ask user input this number or directly assign it to the variable. i included both in the code; so delete one of them.

    Sub RangeSortAndRowHeightChange()
        
        Dim AddXtraRwHt As Double, i As Long
        Dim rng As Range
        Set rng = ActiveCell.CurrentRegion
        
        rng.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlYes
        rng.Rows.AutoFit
        
        AddXtraRwHt = Application.InputBox("Please enter the height that you want", Type:=2)
        AddXtraRwHt = 2
        
        For i = (rng.Rows(1).Row) To (rng.Rows(1).Row + rng.Rows.Count - 1)
            With Cells(i, rng.Columns(1).Column)
                .RowHeight = .RowHeight + AddXtraRwHt
            End With
        Next
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  17. #17
    That is precisely what I was after, it's great.

    I've spent an hour or so sorting various ranges in a few worksheets all over the place and it's perfect.

    Thank you for spending so much time on this it's very much appreciated.

  18. #18
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    just a thought,
    What if you increased the font size, say by 2 or 3 pt for the sheet or activeRegion,
    then ran autosize
    then return the font size to before.

    kind of a work around to get what your after maybe...

    good luck,
    -mark

  19. #19
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @danesrod
    you are welcome.


    @mperrah
    please record a macro while you're doing it manually and share the result.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  20. #20
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    Sorry, should have tested first.
    The size up part works, but when I downsize the font the cell shrinks with it.

    another try is:
    for each row with data add a zero to the last used column plus 1
    then up that font 2 or 4 points and make it the same color as the background (white on white)
    then run the autosize.

    I'll work up a sample of this tomorrow

Posting Permissions

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