+ Reply to Thread
Results 1 to 6 of 6

Range selection using variable

  1. #1
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Atlanta GA
    MS-Off Ver
    2010
    Posts
    108

    Range selection using variable

    I have a program which needs to select a ranges of cells, depending on the value in the variable.

    Let's say the variable is worth 32, which would be the row number and the column is always D, with D32 being the first cell and D48 being the last.

    What would the syntax be to select the range of D32 to D48?
    Please Login or Register  to view this content.
    I know this can't be right, so if you could help, I'd be most thankful

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Range selection using variable

    Hi,

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    ..but remember most of the time it's unnecessary to use .Select - in fact it just slows things down. If you want to use the range for say copying then use

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Atlanta GA
    MS-Off Ver
    2010
    Posts
    108

    Re: Range selection using variable

    Problem is, the end of the range will be 20 cells down from the top of the range. So, it will not always be D48.

    The top could be 210 with the bottom cell being 230.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Range selection using variable

    Quote Originally Posted by miles44 View Post
    Problem is, the end of the range will be 20 cells down from the top of the range. So, it will not always be D48.

    The top could be 210 with the bottom cell being 230.
    But that isn't what you said in post #1 when you said D48 was the last cell. And if the gap is 20 cells how does that square with D32:D48?

    Maybe

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Range selection using variable

    I am bringing up a point which is a pet peeve of mine. On what sheet in the workbook would you like this to happen? It will happen on whatever sheet Excel thinks is active which is sometimes different than the sheet you think is active.

    I recommend doing something like the following:
    Please Login or Register  to view this content.
    Always start your modules with Option Explicit. This forces you to declare your variables and if you go to Debug -> Compile Project it will tell you where you failed to declare a variable. Or the code will tell you when you try to run it. Without Option Explicit, you can misspell a variable name and not know it and you think it has a value but it is actually zero or null or false.

    I like to use shortcuts for sheets for two reasons. The first is obvious: less typing. The second is a bit more subtle: when you assign a sheet, it knows what workbook it belongs to. This is important when the code has more than one workbook open.

    Please note how I explicitly told Excel what sheet to go to for copy and what sheet to go to for paste.

    Shown here is the line: NumRow = shC.Range("B2").

    What this line does is go to cell B2 on the spreadsheet and read the value that is there. So if you are changing the starting line, you can just change the cell value and don't have to change the code.

    As Richard mentioned, avoid using Select when you can. Generally speaking, if you record a macro and it produces code like:
    Please Login or Register  to view this content.
    You can replace it with
    Please Login or Register  to view this content.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  6. #6
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Atlanta GA
    MS-Off Ver
    2010
    Posts
    108

    Re: Range selection using variable

    Richard: Thanks! That got it!
    Sorry about the confusion.

+ 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. Store range of a selection into a variable?
    By eljusticiero67 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-12-2015, 05:05 PM
  2. Troubles with a variable range selection
    By CRDK79 in forum Excel General
    Replies: 2
    Last Post: 03-25-2014, 06:49 PM
  3. [SOLVED] Variable Range Selection
    By ayvee0 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2013, 02:21 PM
  4. Correct syntax for using variable in range selection
    By coasterman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-16-2012, 11:52 AM
  5. Variable Range Selection
    By johngr55 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-02-2010, 03:42 PM
  6. Range selection with variable
    By arora in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2010, 09:53 AM
  7. Range selection from variable start point
    By hriggs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2008, 09:10 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