Consulting

Results 1 to 9 of 9

Thread: VBA multiple ranges

  1. #1

    VBA multiple ranges

    A while ago xld helped me out and created the code below, I modified it to include a named range which I called Info, now I need it to only work on certain cells so have created three discontinuous named ranges Info07, Info08 and Info09 but somehow need to link them togeather and get the code to work on them.

    I think I should be using Union but not sure how to make it work with the code.

    Any help gratefully recieved even if only being pointed to another website.

    Thanks (from long time lurker)



    [VBA]Private Sub Worksheet_Calculate()
    Dim Target As Range
    Dim icolor As Long
    On Error Resume Next
    Application.DisplayAlerts = False
    For Each Target In Me.Range("E17:P51")
    icolor = xlColorIndexNone
    Select Case Target
    Case 0 To 0: [White]
    Case 1 To 75: icolor = 3
    Case 76 To 95: icolor = 26
    Case 96 To 100: icolor = 45
    Case 101 To 102: icolor = 46
    Case 103 To 105: icolor = 4
    Case 106 To 1000: icolor = 50
    Case Else: 'Whatever
    End Select

    Target.Interior.ColorIndex = icolor
    Next Target
    End Sub


    [/VBA]

  2. #2
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    [uvba][vba]<insert your code here>[/vba][/uvba]

    Your code will then look like this:[vba]Private Sub Worksheet_Calculate()
    Dim Target As Range
    Dim icolor As Long

    On Error Resume Next
    Application.DisplayAlerts = False

    For Each Target In Me.Range("E17:P51")

    icolor = xlColorIndexNone

    Select Case Target
    Case 0 To 0: [White]
    Case 1 To 75: icolor = 3
    Case 76 To 95: icolor = 26
    Case 96 To 100: icolor = 45
    Case 101 To 102: icolor = 46
    Case 103 To 105: icolor = 4
    Case 106 To 1000: icolor = 50
    Case Else: 'Whatever
    End Select

    Target.Interior.ColorIndex = icolor

    Next Target

    End Sub
    [/vba]

    Thanks,
    Ron
    Windermere, FL

  3. #3

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Calculate()
    Dim Target As Range
    Dim icolor As Long
    On Error Resume Next
    Application.DisplayAlerts = False
    For Each Target In Union(Me.Range("Info07"), Me.Range("Info08"), Me.Range("Info09"))

    icolor = xlColorIndexNone
    Select Case Target

    Case 0 To 0: [White]
    Case 1 To 75: icolor = 3
    Case 76 To 95: icolor = 26
    Case 96 To 100: icolor = 45
    Case 101 To 102: icolor = 46
    Case 103 To 105: icolor = 4
    Case 106 To 1000: icolor = 50
    Case Else: 'Whatever
    End Select

    Target.Interior.ColorIndex = icolor
    Next Target
    End Sub
    [/vba]
    ____________________________________________
    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

  5. #5
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Mad_Kitty,

    Don't worry about it. This is one of those things we each learn as we begin using the website. Your code is short an sweet; the challenge is when someone posts 2 or 3 screensful of code without using the VBA tags. That much code, all flush left, quickly dampens others' enthusiam for helping the original poster (aka OP).

    Cheers,
    Ron
    Windermere, FL

  6. #6
    One thing I have picked up on is any longer and you upload example so people know what your talking about.

  7. #7
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Ah, yes. Around here everything goes better with ... a sample workbook. (borrowing from someone's advertising slogan)


    Thanks,
    Ron
    Windermere, FL

  8. #8
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    XLD,

    I assume that one could have specified the subranges in an absolute format. The advantage to named ranges are that one's code is less cluttered, and the named ranges will automatically expand (or shrink) whenever a named range is affected by row and/or column inserts and deletions?

    Thanks,
    Ron
    Windermere, FL

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, and the OP did mention named ranges.

    Also, names are part of Excel, so it is clearer to most people that they need changing, whereas hard-coding may not.
    ____________________________________________
    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
  •