Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Excel VBA code range for multiple cells

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Intersect(Target, Range("$C$17:$C$80")) Is Nothing Then Exit Sub
    Select Case Target
    Case ""
        Target = "Priority 1"
        Target.Interior.ColorIndex = 3
    Case "Priority 1"
        Target = "Priority 2"
        Target.Interior.ColorIndex = 6
    Case "Priority 2"
        Target = "Priority 3"
        Target.Interior.ColorIndex = 45
    Case Else
        Target = ""
        Target.Interior.ColorIndex = 15
    End Select
    Cancel = True
End Sub

I am having issues with my range. I need to expand my range to only specific cells. How would I go about doing this? The cells I need selected are,

C17,C21,C25,C29,C33,C42,C46,C50,C54,C58,C67,C71,C75,C83,C87,C91,C100,M17,M21,M25,M29,M33,M42,M46,M55,M59,M69,M73,M82,M86,W17,W21,W25,W29,AG17,AG21,AG25,AG34,AG38,AG42,AG51,AG55,AG59,AG68,AG72,AG81,AG85,AG89,AG98,AG102,AG106,AG110,AG114,AQ17,AQ21,AQ30,AQ34,AQ43,AQ47,AQ51,AQ60,AQ64,BA17,BA21,BA25,BA29,BA33,BA37,BA46,BA50,BA54,BA58,BA62,BA71,BA75,BA79,BA89,BA93".

The above cells need priority selections with color change and the cells below need only a color change for a status update. How do I go about doing this as well?

C19,C23,C27,C31,C35,C44,C48,C52,C56,C60,C69,C73,C77,C85,C89,C93,C102,M19,M23,M27,M31,M35,M44,M48,M57,M61,M71,M75,M84,M88,W19,W23,W27,W31,AG19,AG23,AG27,AG36,AG40,AG44,AG53,AG57,AG61,AG70,AG74,AG83,AG87,AG91,AG100,AG104,AG108,AG112,AG116,AQ19,AQ23,AQ32,AQ36,AQ45,AQ49,AQ53,AQ62,AQ66,BA19,BA23,BA27,BA31,BA35,BA39,BA48,BA52,BA56,BA60,BA64,BA73,BA77,BA81,BA91,BA95"

like image 580
C. Cons Avatar asked Feb 18 '26 19:02

C. Cons


2 Answers

You would put what you typed directly in the range statement. As stated because of vba's limit of 255 characters in the range statement you would need to use the union statement

Union(Range("C17,C21,C25,C29,C33,C42,C46,C50,C54,C58,C67,C71,C75,C83,C87,C91,C100,M17,M21,M25"), _
            Range("M29,M33,M42,M46,M55,M59,M69,M73,M82,M86,W17,W21,W25,W29,AG17,AG21,AG25,AG34,AG38,AG42,AG51,AG55"), _
            Range("AG59,AG68,AG72,AG81,AG85,AG89,AG98,AG102,AG106,AG110,AG114,AQ17,AQ21,AQ30,AQ34,AQ43,AQ47,AQ51,AQ60"), _
            Range("AQ64,BA17,BA21,BA25,BA29,BA33,BA37,BA46,BA50,BA54,BA58,BA62,BA71,BA75,BA79,BA89,BA93"))

You would then use a else if to do the second

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Union(Range("C17,C21,C25,C29,C33,C42,C46,C50,C54,C58,C67,C71,C75,C83,C87,C91,C100,M17,M21,M25"), _
            Range("M29,M33,M42,M46,M55,M59,M69,M73,M82,M86,W17,W21,W25,W29,AG17,AG21,AG25,AG34,AG38,AG42,AG51,AG55"), _
            Range("AG59,AG68,AG72,AG81,AG85,AG89,AG98,AG102,AG106,AG110,AG114,AQ17,AQ21,AQ30,AQ34,AQ43,AQ47,AQ51,AQ60"), _
            Range("AQ64,BA17,BA21,BA25,BA29,BA33,BA37,BA46,BA50,BA54,BA58,BA62,BA71,BA75,BA79,BA89,BA93"))) Is Nothing Then
    Select Case Target.Value
        Case ""
            Target = "Priority 1"
            Target.Interior.ColorIndex = 3
        Case "Priority 1"
            Target = "Priority 2"
            Target.Interior.ColorIndex = 6
        Case "Priority 2"
            Target = "Priority 3"
            Target.Interior.ColorIndex = 45
        Case Else
            Target = ""
            Target.Interior.ColorIndex = 15
    End Select
ElseIf Not Intersect(Target, Union(Range("Your Second Group"))) Is Nothing Then
   'do your second stuff here
End If
Cancel = True
End Sub

Also, as @matthewD stated in the comments, you could name both ranges and refer to the name instead. This has a huge advantage if you will refer to these two specific ranges multiple times. If only the once then it is the same.

As this has been changed to reflect input from others I have changed it to a community wiki.

like image 103
3 revsScott Craner Avatar answered Feb 21 '26 08:02

3 revsScott Craner


String constants cannot be longer than 255 characters in VBA. You've got to split the statement up:

Dim HotArea As Range

Set HotArea = Range("C17,C21,C25,C29,C33,C42,C46,C50,C54,C58,C67,C71,C75,C83,C87,C91,C100,M17")
Set HotArea = Union(HotArea, Range("M21,M25,M29,M33,M42,M46,M55,M59,M69,M73,M82,M86,W17,W21,W25,W29,AG17,AG21,AG25,AG34"))
Set HotArea = Union(HotArea, Range("AG38,AG42,AG51,AG55,AG59,AG68,AG72,AG81,AG85,AG89,AG98,AG102,AG106,AG110,AG114,AQ17"))
Set HotArea = Union(HotArea, Range("AQ21,AQ30,AQ34,AQ43,AQ47,AQ51,AQ60,AQ64,BA17,BA21,BA25,BA29,BA33,BA37,BA46,BA50,BA54"))
Set HotArea = Union(HotArea, Range("BA58,BA62,BA71,BA75,BA79,BA89,BA93"))

If Not Intersect(Target, HotArea) is Nothing Then
...  

or alternatively in one statement

    Set HotArea = Union(Range("C17,C21,C25,C29,C33,C42,C46,C50,C54,C58,C67,C71,C75,C83,C87,C91,C100,M17"), _
            Range("M21,M25,M29,M33,M42,M46,M55,M59,M69,M73,M82,M86,W17,W21,W25,W29,AG17,AG21,AG25,AG34"), _
            Range("AG38,AG42,AG51,AG55,AG59,AG68,AG72,AG81,AG85,AG89,AG98,AG102,AG106,AG110,AG114,AQ17"), _
            Range("AQ21,AQ30,AQ34,AQ43,AQ47,AQ51,AQ60,AQ64,BA17,BA21,BA25,BA29,BA33,BA37,BA46,BA50,BA54"), _
            Range("BA58,BA62,BA71,BA75,BA79,BA89,BA93"))

as Union() takes an arbitrary number of arguments.

like image 23
user1016274 Avatar answered Feb 21 '26 08:02

user1016274



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!