+ Reply to Thread
Results 1 to 9 of 9

Setting a range based on variables

  1. #1
    Registered User
    Join Date
    11-17-2011
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    43

    Setting a range based on variables

    I am trying to set a range to be manipulated using 4 integer variables that denote the top of the range cell row/column and the bottom of the range row/column. The range will always be one column wide but will be a varying number of rows long. I have tried various similar situations I have found on various forums, but I always seem to end up with a ‘Run-time error ‘1004’: Method ‘Range;’ of object ‘_Global’ failed. I have tried a few things and now I think I finally am ready to ask for help… how can I set variable rng to be “[rTop][cTop]:[rBottom][cBottom]” (cell example – “A34:A54”)

    Please Login or Register  to view this content.

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Setting a range based on variables

    I don't know where you are heading with this code but I doubt you'll ever get anywhere. Sorry to be that hard on you. Here are some explanations:

    First of all, you must SET a range as in:
    Please Login or Register  to view this content.
    remark the use of the function Cells which needs 2 parameters (rows and columns) as compared to the Range one which needs an address (like "A2").

    Not sure at all but is it that you want to ask the operator to mark the limits of that range? Why? Don't you know where it starts? Ends?

    How does you sheets looks like? It would hlep us to help you on that.
    Regards
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Setting a range based on variables

    You could use:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Setting a range based on variables

    Note that in your Dim statements, only the last/rightmost variable is defined. The others are all variants. It is usual to define row variables as Long.


    Regards, TMS

  5. #5
    Registered User
    Join Date
    11-17-2011
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Setting a range based on variables

    Quote Originally Posted by TMShucks View Post
    You could use:

    Please Login or Register  to view this content.

    Regards, TMS
    This is giving me a type mismatch. It essentially is trying to set rng as Cell(value in top cell, value in bottom cell) isntead of Cell (cell directory of top cell, cell directory of bottom cell)

    Below is a picture of different points of the debug that shows what it is doing. here are my declarations:
    Dim rTop As Integer
    Dim rBottom As Integer
    Dim cTop As Integer
    Dim cBottom As Integer

    Dim rng As Range

    vendor mismatch.png

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

    Re: Setting a range based on variables

    I think that TMShucks meant
    Please Login or Register  to view this content.
    Also since there are more rows in an Excel sheet than there are integers, all those variables should be dimmed as Long.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Setting a range based on variables

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-17-2011
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: Setting a range based on variables

    Thank you everyone. changing the variables to long, changing the first cell to Range and using rng.address fixed every problem.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Setting a range based on variables

    You're welcome.

+ 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