简体   繁体   English

确定多个单元格的Excel VBA代码范围

[英]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". 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" 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"

You would put what you typed directly in the range statement. 您可以将您键入的内容直接放在range语句中。 As stated because of vba's limit of 255 characters in the range statement you would need to use the union statement 如上所述,由于vba在range语句中限制为255个字符,因此您需要使用union语句

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 然后,您将使用else if第二个

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. 另外,正如@matthewD在评论中所述,您可以命名两个范围并改为引用该名称。 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. 由于这已经改变以反映来自其他人的输入,我已将其更改为社区维基。

String constants cannot be longer than 255 characters in VBA. VBA中的字符串常量不能超过255个字符。 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. 因为Union()接受任意数量的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM