+ Reply to Thread
Results 1 to 14 of 14

UsedRange in a Selection

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    UsedRange in a Selection

    Wondering it's possible to use the UsedRange property within a selection. For instance, if I have a long list of Data in column A, and select all of column A, but want my macro to only operate on the cells with values in my selection, can I use the UsedRange property, or is there a better way? This comes up because I was using my little macro below and accidentally selected the whole column, so the macro took 55 seconds to run, when normally it runs very quickly.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: UsedRange in a Selection

    If you use select, you need to select the range manually.

    Please Login or Register  to view this content.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: UsedRange in a Selection

    Try specialcells....

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    I think I solved it using intersect, plus bringing in davesexcel's excellent suggestion. let me know if you see anything that can be improved upon with my code below.
    Please Login or Register  to view this content.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: UsedRange in a Selection

    Hi,

    There is no real benefit to finding the intersection of Usedrange with Specialcells, since specialcells only applies to the used range anyway.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    So then this would be an equivalent macro?
    Please Login or Register  to view this content.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: UsedRange in a Selection

    Yes it would. Either way, you should test to be sure that more than one cell is selected, or Specialcells will apply to the whole used range of the sheet.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: UsedRange in a Selection

    I would caution about the use of SpecialCells.

    If the Selection is multiple cells, SpecialCells will return those cells in the Selection that meet the criteria specified by the arguments.

    If Selection is a single cell, SpecialCells will return all of the matching cells from the whole sheet.

    Please Login or Register  to view this content.
    Last edited by mikerickson; 03-23-2017 at 10:05 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    Mike,
    Thanks for the warning. I fixed as shown below.
    Please Login or Register  to view this content.
    I liked your example macro, but slightly reworked it for dummies like me that have to see what's happening to understand the problem.
    Please Login or Register  to view this content.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: UsedRange in a Selection

    One sub-text of my example is that Selecting is a bad idea unless nessesary.
    It takes time.
    It requires the screen do something.
    And, if the wrong sheet is Active, it will crash a routine.

    Its better practice to use VBA without selecting. (Unless needed, like your above example to provide a visual aid for the user.)

  11. #11
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    I guess I didn't explain the purpose of the macro. Suppose JoeBob sends some data, and one section/colum/row/range area has the values reversed (this happens to me all the time), so that range needs to be converted. Easiest way is to select the range to convert and hit the "Easy" button (ie run my PlusMinus macro).

    If my range size was always the same I wouldn't do any selecting, I'd just load the macro with the proper range.

    I COULD have an interface asking for the range to convert, but that involves an extra step for the user, and accomplishes no purpose that couldn't be more easily done by simply selecting the range to convert and running the macro. The whole idea is a quick and easy conversion of whatever range the user needs to convert.

  12. #12
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    Just realized you may have been talking about the changes I did to your macro.

    To me, to demonstrate something, you want to show it, so to show what's happening you either need 1000 words, or a picture. So in Test I stop the macro so the user (Me, in this case) can see what's happening. When you can see that only A4 is selected, yet the msgbox displays "$A$1:$A$4,$A$6:$A$10", it's an easy picture to see that there's a problem.

  13. #13
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: UsedRange in a Selection

    No, I was talking in general.
    Your particular use is a good user interface.

    But often on these boards, we get questions from people who have learned from the MacroRecorder and don't know that all that selecting isn't needed in VBA.

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: UsedRange in a Selection

    I agree wholeheartedly. It was a wonderful day for me when I realized I could do magic without "touching" anything on my sheets.

+ 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. [SOLVED] Undo "ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value" after copy
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2016, 06:35 AM
  2. [SOLVED] sht usedrange help
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-31-2015, 05:22 PM
  3. [SOLVED] usedrange doesn't reset when using 'application.activesheet.usedrange'
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2015, 10:39 PM
  4. UsedRange
    By LB79 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-08-2010, 08:39 AM
  5. [SOLVED] USEDRANGE
    By ak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-05-2006, 12:15 AM
  6. [SOLVED] usedRange
    By GC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2005, 09:06 AM
  7. [SOLVED] usedrange
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-18-2005, 10:06 AM

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