Results 1 to 9 of 9

Thread: Creating a Range (VBA/Excel)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Creating a Range (VBA/Excel)

    Can someone tell me how I can initialise a Range so it defines a (rectangular) region on the spreadsheet? I want to be able to define it in terms of the top-left corner and bottom-right corner or, failing that, the top-left corner and the width and height of the range.

    I've been trying to use the Offset and Resize methods of the Range class, but whilst the Offset method works, the Resize method doesn't seem to exist outside of the few examples I've found on the internet.

    Thanks in advance,
    Barguast
    Using Visual Studio .NET 2005

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

    Re: Creating a Range (VBA/Excel)

    What is the intent here? Any range of cells will be a rectangle
    Declan

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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Creating a Range (VBA/Excel)

    Quote Originally Posted by DKenny
    What is the intent here? Any range of cells will be a rectangle
    Yes I know, but I just want to know how to create a Range object from non-constant cell coordinates. For example how would I create a range object which began in cells 'x', 'y' and was 'w' in width and 'h' in height?

    The only way I know of creating a Range object is with an Excel-style name such as "A1:B5".
    Using Visual Studio .NET 2005

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

    Re: Creating a Range (VBA/Excel)

    Try this.

    The idea being to keep adding cells to the right of the range until I get to the desired width, then adding cells to the bottom of the range until I get to the desired height.


    VB Code:
    1. Sub BarguastRange()
    2. Dim RWidth As Long
    3. Dim RHeight As Long
    4. Dim Result As Range
    5.  
    6.     RWidth = 1000
    7.     RHeight = 250
    8.    
    9.     Set Result = Range("A1")
    10.    
    11.     Do While Result.Width < RWidth
    12.         Set Result = Application.Union(Result, Result.Offset(0, 1))
    13.     Loop
    14.     Do While Result.Height < RHeight
    15.         Set Result = Application.Union(Result, Result.Offset(1, 0))
    16.     Loop
    17.    
    18.     Result.Select
    19.    
    20. End Sub
    Declan

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

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Creating a Range (VBA/Excel)

    You can use R1C1 notation to specify a range, eg:

    "R1C1:R5C2" (="A1:B5")

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Creating a Range (VBA/Excel)

    Quote Originally Posted by si_the_geek
    You can use R1C1 notation to specify a range, eg:

    "R1C1:R5C2" (="A1:B5")
    That might be the best method. Something along the lines of...
    Code:
    Private Function GetRange (x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer) as Range
    
        Dim rangestr as String
        range = "R" + CStr (y1) & "C" & CStr (x1) & ":R" & CStr (y2) & "C" * + Cstr (x2)
    
        GetRange = Sheet.Range (rangestr)
    
    End Function
    ... presumably? Still, I'd have thought there was a better way. Did the Range class even have a Resize method? As I mentioned in my first post, I've seen it used in example on the internet, but the method doesn't seem to exist when I try to use it.
    Using Visual Studio .NET 2005

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

    Re: Creating a Range (VBA/Excel)

    I thought you wanted a specifc width and height, but what you want is a specific number of cells, right?
    If so then the following would work.

    VB Code:
    1. Function GetRange(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) As Range
    2.     GetRange = Range(Cells(x1, y1), Cells(x1, y1).Offset(x2, y2))
    3. End Function
    Declan

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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Creating a Range (VBA/Excel)

    Quote Originally Posted by DKenny
    I thought you wanted a specifc width and height, but what you want is a specific number of cells, right?
    If so then the following would work.

    VB Code:
    1. Function GetRange(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) As Range
    2.     GetRange = Range(Cells(x1, y1), Cells(x1, y1).Offset(x2, y2))
    3. End Function
    That is perfect.

    Many thanks to you both.
    Using Visual Studio .NET 2005

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: Creating a Range (VBA/Excel)

    Urgh, sorry to double post, but this ought to be a simple enough question.

    How can I (programatically) set the viewable range of the Worksheet? There is a 'Viewable Range' field in the 'Commands and Options' form in Design View which works perfectly fine, but I want to be able to alter the viewable range whilst the program is running.
    Using Visual Studio .NET 2005

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