Consulting

Results 1 to 9 of 9

Thread: Set Range Using Variables

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    Set Range Using Variables

    I need to use variables to define a range. Sorry as this is very basic, but can't get to work.

    Dim ws As Worksheet
    Set ws = Worksheets("Sheet 1")
    
    'This works:
       ws.Range("A1:C10").ClearContents
    
    
    'But tried variations similar to below using variables and not working:
        r1 = "A" & RowVariable1
        r2 = "C" & RowVariable2
    
    ws.Range("r1:r2").ClearContents
    How can I set a range using variables r1 & r2 ?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ws.Range(r1 & ":" & r2).ClearContents
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You could also use

    ws.Range(r1, r2).ClearContents
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Excellent! works, thanks very much

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by BrI View Post
    I need to use variables to define a range. Sorry as this is very basic, but can't get to work.

    How can I set a range using variables r1 & r2 ?

    I think it'll depend on how r1 and R2 are Dim-ed, that is either as Range or as String (or maybe Variant since your macro doesn't Dim them)




    Option Explicit
    Sub AsString()
        Dim ws As Worksheet
        Dim r1 As String, r2 As String
        Dim RowVariable1 As Long, RowVariable2 As Long
        Set ws = Worksheets("Sheet1")
     
        'This works:
        'ws.Range("A1:C10").ClearContents
     
        RowVariable1 = 1
        RowVariable2 = 15
     
     
        'But tried variations similar to below using variables and not working:
        r1 = "A" & RowVariable1
        r2 = "C" & RowVariable2
     
        ws.Range(r1 & " :" & r2).ClearContents
    End Sub
    
    
    Sub AsRange()
        Dim ws As Worksheet
        Dim r1 As Range, r2 As Range
        Dim RowVariable1 As Long, RowVariable2 As Long
        Set ws = Worksheets("Sheet1")
     
        'This works:
        'ws.Range("A1:C10").ClearContents
     
        RowVariable1 = 1
        RowVariable2 = 15
     
     
        'But tried variations similar to below using variables and not working:
        Set r1 = ws.Range("A" & RowVariable1)
        Set r2 = ws.Range("C" & RowVariable2)
        
        Range(r1, r2).ClearContents
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Thanks, I had been wondering how to Dim the variables. I went with a string and (r1, r2) worked with the string Dim.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Paul_Hossler View Post
    I think it'll depend on how r1 and R2 are Dim-ed, that is either as Range or as String (or maybe Variant since your macro doesn't Dim them)
    If they had been range objects, he would have gotten a compile error as he isn't Set'ting them.
    Last edited by Paul_Hossler; 06-12-2017 at 02:51 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by xld View Post
    If they had been range objects, he would have gotten a compile error as he isn't Setting them.
    True, but all I saw was "Can't get it to work" and "How can I set a range using ..." so I went with a "wordy, try to cover all bases" suggestion
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Although I quoted you Paul, it was intended more as a corollary to your statements, for the OP's edification
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •