+ Reply to Thread
Results 1 to 17 of 17

select range

  1. #1
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Question select range

    I need to select a range, I have this code

    Please Login or Register  to view this content.
    Basically I need to select a range of cells which could be from A2:someColumnLetter & SomeRow#

    Dont think the above code quite works?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    First, if possible, try not to use Usedrange to spot row and column maximums. In most cases it's fine, but there are situations where the results are not correct. If you can specify a starting point reliably, do so.

    Here are my two most common approaches to getting LR and LC variables filled in:
    Please Login or Register  to view this content.
    To answer your question, the LC variable returns a numeric answer. So you'll need to switch to the Cells() function so that you can use a numeric value for the column portion.

    Please Login or Register  to view this content.
    Lastly, you didn't show what happens next...but when writing code by hand (or editing macro recorder code) there is seldom ever a need to "select" a range to do what you want. If you select, the next code invariably starts with Selection.Copy or whatever.

    There's no need. Skip the selecting and give the command directly:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    Thanks that code worked with a small modification (added -4 to end of column)

    See attachment
    Now see if I Click on "Hide/Unhide $" and then click on "Copy", open Word and paste it in there, the table goes off the page.

    But then if I click "Hide/Unhide K" and click "Copy" and then open word and paste that there it works fine keeping the formatting.

    How can I fix that?
    Attached Files Attached Files

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: select range

    Are you saying that yo are trying to paste to Word/ If so why not just print the workbook?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    Well because ill paste into an already formated document, im just taking that data into a word file that I have read

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,331

    Re: select range

    FYI, using Usedrange and SpecialCells(xlCellTypeLastCell) will return the same 'last' cell and hence are both subject to the same potential error.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    But how come formatting is not kept when I paste into word?
    Could be because I am hiding cells.
    Last edited by philmetz; 08-24-2009 at 05:26 AM.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    Quote Originally Posted by romperstomper View Post
    FYI, using Usedrange and SpecialCells(xlCellTypeLastCell) will return the same 'last' cell and hence are both subject to the same potential error.
    I'm not sure this is correct. Based on the OPs original technique, if a sheet ONLY has values in cells C4:E5, the LastR and LastC values come up to be:
    Please Login or Register  to view this content.
    Since the last cell with data is E5, the answers should have been 5 and 5, so this technique does not work when cell A1 is empty.

    This next technique always spots the last used cell in sheet and gives the proper values.
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 08-24-2009 at 11:55 AM.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,331

    Re: select range

    I think you missed my point, so perhaps I wasn't clear. UsedRange and xlCellTypeLastCell both suffer from the problem you mentioned whereby they can include cells that are no longer actually in use (or perhaps that was not what you were referring to?) In other words, the bottom right cell of UsedRange is what xlCellTypeLastCell refers to. Both are inaccurate on occasion.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    No, the error I was point out is that UsedRange.Columns.Count will count the number of columns with data in it, not tell you what the last column with any data in it is. UsedRange.Rows.Count...same thing. My example above demonstrates the incorrect results.

    I understand what you mean, but xlCellTypeLastCell is still a usable technique for spotting the lastrow and lastcolumn for the entire sheet. When you're trying to insure you get all the data on the sheet, at least this does always work.

    For finesse range copying, I would use a more surgical selection technique, anyway, but this will always work for getting it ALL, whereas the OPs original technique could/would most definitely return values far too small.

    If you think the xlCellTypeLast has a demonstrable flaw that results in data not getting including, please explain. I'd hate to have a land mine on this, as you've warned.

  11. #11
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    umm so how come when I click copy and paste in MS Word, the description column width is made wider a bit? How can i stop this?
    Attached Files Attached Files

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    Excel and Word have a lot of their own "display" characteristics. Pasting loose data from one to the other will result in some shifting, at least it always has for me.

    The only way I ever insure the Word version is digit-for-digit identical when pasted is to pastespecial as a Picture.

    NOTE: The copy technique used in your sheet will work fine since there are no blank rows/columns in your data, but as explained above, this WILL bite you one day and worse, you might not know it has. I would adjust to a different LR/LC technique now so it can never happen to you.

  13. #13
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    what you mean LR LC?

    so theres no way of copying with the formatting? hmm ok
    Last edited by philmetz; 08-24-2009 at 01:27 PM.

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    Quote Originally Posted by philmetz View Post
    what you mean LR LC?
    Have you been following the discussion above regarding the technique you are using for determine last row (LR) and last column (LC) in your sheet. As noted, your technique works for this sheet, but the technique has a major flaw in it of which you should be aware.

    so theres no way of copying with the formatting? hmm ok
    That's not what I said, it is copying with the formatting, but once it arrives in Word, if it's loose text, Word can adjust it. The formatting is all still there, but column widths can be affected by the way the programs are displaying data.

  15. #15
    Forum Contributor
    Join Date
    06-17-2007
    Posts
    113

    Re: select range

    yea thats what I ment, i cant keep the width at the same amount in excel>?

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,331

    Re: select range

    Quote Originally Posted by JBeaucaire View Post
    If you think the xlCellTypeLast has a demonstrable flaw that results in data not getting including, please explain. I'd hate to have a land mine on this, as you've warned.
    No, quite the reverse - when it's wrong, you end up processing more cells than required, not fewer.
    You can use Usedrange but you need to add .Row to .Rows.Count and subtract 1 and similarly with the columns. That at least doesn't require you to know the start cell, but again can return too large a range.

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: select range

    Well, we're discussing mildly related issues. As I'd indicated, I would be more surgical in my method of determining LR and LC for specific ranges, but when an entire sheet's worth of data is being sought, truly the last cell in the sheet, then xlCellTypeLastCell is fine. Can extra cells be included, yes, absolutely, but that's not nearly the problem that too little data being processed creates.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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