Results 1 to 5 of 5

Thread: Excel Macro Special Cell Selection

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Excel Macro Special Cell Selection

    Your help here would be great .....

    I am trying to use an excel macro to select 2 ranges on the same sheet:
    - the first range is A1:G56
    - the second range is I59 to the last active cell. This last active cell is always in column I and is always at least 10 rows greater than row 59.
    The problem is that the macro that excel generates (below) always selects A1:I(x) where x is the last row number. eg if the last active cell in column I is 90, A1:I90 is selected. The ranges I do not need selected are A57:G90 and I1:I55.

    Range("A1:G56,I59").Select
    Range("I59").Activate
    Range(Selection, ActiveCell.SpecialCells(xlCellTypeLastCell)).Select
    Range(Selection, Selection.End(xlUp)).Select

    Would really appreciate your help on this one.
    Cheers
    Marc

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Excel Macro Special Cell Selection

    Hmm had a go. Weirdly doesn't like me make multiple range seklections using the range command :/

    Guess it only works if you specify via address type (such as the recording you made already).

    sorry couldn't help

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Special Cell Selection

    You cna use the Union Method to combine multiple ranges in excel. Here's some code that will union the ranges that you have described.

    VB Code:
    1. Sub MultiRangeSelect()
    2. Dim rngTopSection As Range
    3. Dim rngBottomStart As Range
    4. Dim rngBottomSection As Range
    5. Dim rngCombined As Range
    6.    
    7.     Set rngTopSection = ThisWorkbook.Worksheets(1).Range("A1:G56")
    8.    
    9.     Set rngBottomStart = ThisWorkbook.Worksheets(1).Range("I59")
    10.     Set rngBottomSection = Range(rngBottomStart, rngBottomStart.End(xlDown))
    11.    
    12.     Set rngCombined = Application.Union(rngTopSection, rngBottomSection)
    13.    
    14.     rngCombined.Select
    15.    
    16. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    Re: Excel Macro Special Cell Selection

    Or
    VB Code:
    1. Sub SelectRange()
    2.  
    3. Dim r1 As Range, r2 As Range
    4.  
    5. Set r1 = ActiveSheet.Range("A1:G56")
    6. Set r2 = ActiveSheet.Range("I59", Cells.SpecialCells(xlCellTypeLastCell))
    7. ActiveSheet.Range(r1.Address & "," & r2.Address).Select
    8.  
    9. End Sub
    BUT DKenny's solution is better (doesnt involve string concat operations)
    Last edited by bilm_ks; Feb 13th, 2006 at 10:35 AM.
    "bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Smile Re: Excel Macro Special Cell Selection

    It works perfectly both ways - thank you both very much for your help - this has helped me put the finishing touches to a pricing system I have developed for work.
    Cheers
    Marc

Posting Permissions

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



Click Here to Expand Forum to Full Width