Consulting

Results 1 to 6 of 6

Thread: Set rng as Multiple Ranges

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Set rng as Multiple Ranges

    I want to use the following code, but I cannot find an example of setting the range to multiple ranges, while breaking up the line of code.


    Set rng = Range("F19:F37,P19:P37,F46:F64,P46:P65,F73:F91,F136:F154,

    Yes, it continues for about a mile. How do I break up that line in my VBE window?

    I tried space underscore, but it doesn't seem to like that.
    ~Anne Troy

  2. #2
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Anne,
    I know you have many more ranges to add, but I'll use the range you provided as an example. Since "F19:F37,P19:P37,F46:F64,P46:P65,F73:F91,F136:F154" is really just a string, you would use normal concatenation for it, like:

    Set Rng = Range("F19:F37," & _
     "P19:P37," & _
     "F46:F64," & _
     "P46:P65," & _
     "F73:F91," & _
     "F136:F154")
    Or you could use the union statement, like:

    Set Rng = Union([f19:f37], _
     [p19:p37], [f46:f64], _
     [p46:p65], [f73:f91], _
     [f136:f154])
    Or even a combination of union and intersect (union for the 2 columns, intersect to get the intersection of those columns and the specific rows):

    Set Rng = Intersect(Range("19:37,46:64,73:91,136:154"), _
     Union(Columns("F"), Columns("P")))
    Matt

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Quote Originally Posted by mvidas
    Hi Anne,
    I know you have many more ranges to add, but I'll use the range you provided as an example. Since "F19:F37,P19:P37,F46:F64,P46:P65,F73:F91,F136:F154" is really just a string,
    That's the ticket. THANKS!!!!
    ~Anne Troy

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    If it is always F and P for the same rows, you'll probably find it easier to use the Intersect and Union method, as it would be less things to change later on if you need to and easier (to my eyes, at least) to read.
    Glad to help!

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    It's not.

    I get an error when I run this:

    Dim rng As Range
    Dim cll As Range
    Dim howmuch
      howmuch = InputBox("By how much shall we multiply the XXX range?", "Enter Value", 0)
      If Not IsNumeric(howmuch) Then
        MsgBox "Invalid entry.", vbOKOnly + vbExclamation, "Error"
        Exit Sub
      End If
        Set rng = Range("F19:F37" & _
        "P19:P37" & _
        "F46:F64" & _
        "P46:P65" & _
        "F73:F91" & _
        "F136:F154" & _
        "P136:P154" & _
        "F163:F181" & _
        "P163:P181" & _
        "F190:F208" & _
        "F252:F271" & _
        "P252:P271" & _
        "F280:F298" & _
        "P280:P298" & _
        "F487:F516" & _
        "P487:P516" & _
        "F525:F554" & _
        "F604:F629" & _
        "P605:P629" & _
        "F638:F663" & _
        "P638:P663" & _
        "F721:F763" & _
        "P721:P763")
    Application.ScreenUpdating = False
      For Each cll In rng.Cells
        cll.Value = cll.Value + howmuch
      Next cll
      Application.ScreenUpdating = False
    End Sub
    Error: method range of object global failed

    ~Anne Troy

  6. #6
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Looks like you forgot some commas in there to separate the individual ranges

    Also, as I personally don't like a lot of lines of code (even though my example to you showed those), you could condense yours down to
    Set rng = Range("F19:F37,P19:P37,F46:F64,P46:P65,F73:F91,F136:F154,P136:P154," & _
     "F163:F181,P163:P181,F190:F208,F252:F271,P252:P271,F280:F298,P280:P298,F487:F516," & _
     "P487:P516,F525:F554,F604:F629,P605:P629,F638:F663,P638:P663,F721:F763,P721:P763")

Posting Permissions

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