Results 1 to 2 of 2

Thread: Picking Rows based on initially selected cells and using VBA

  1. #1

    Picking Rows based on initially selected cells and using VBA



    Register for a FREE account, and/
    or Log in to avoid these ads!

    I'm using MS Excel for Mac 2011.

    I want to pick a contiguous group of cells in my Excel spreadsheet, then run a macro. Note that the number of rows picked could be highly variable, and the first row picked could be highly variable.

    I want that macro to know which rows I've picked, and sort a DIFFERENT group of contiguous cells in the same exact rows as I picked before running the macro. And then sort again on yet another different group of contiguous cells in the same exact rows as I picked before running the macro. In both "sorts" I plan to hard code in the columns for sorting.

    I don't understand Excel VBA "grammar" at all. I learned BASIC a long time ago, but VBA is different, and I forgot a lot of BASIC anyhow.

    I've tried things for a couple days and gotten nowhere. I'm stuck on how to transfer the starting row and final row into VBA. The sorting code is not the problem, since I can just "Record Macro" and get the commands for that.

    I have tried Range("D"&(ActiveCell.Row,1).Select
    That gets me to the current row at least, but ActiveCell.RowHeight doesn't seem to get me to the last row. If Selection.Offset is the answer, I'm not doing it right.

    (My first post. Assume I know none of the lingo! For instance: I tried googling for answers but I must be using the words "selection" and "range" incorrectly.)

    Axel

    P.S. The contents of the cells are not important.

    If I pick D23:E26, then run the macro, I want the computer to sort on F23:H26, then sort on A23:A26, then sort on A23:H26.

    Then in my original spreadsheet I might next pick F34:G256, then run the macro, and I want the computer to sort on F34:H256, then sort on A34:A256, then sort on A34:H256.

    And so on.
    Last edited by Axel_Lerner; 2015-03-22 at 05:59 PM. Reason: example

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,512
    Articles
    0
    Excel Version
    365
    An illustration of one way to do it:
    Code:
    Sub blah()
    Dim OrigSelection As Range, Range1 As Range, Range2 As Range, Range3 As Range
    Set OrigSelection = Selection
    Set Range1 = Intersect(OrigSelection.EntireRow, Range("F:H"))
    Set Range2 = Intersect(OrigSelection.EntireRow, Range("A:A"))
    Set Range3 = Intersect(OrigSelection.EntireRow, Range("A:H"))
    Range1.Select
    Range2.Select
    Range3.Select
    OrigSelection.Select
    End Sub

Tags for this Thread

Posting Permissions

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