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
我的范围问题。我需要将我的范围扩展到特定的细胞。我该怎么做呢?我需要选择的细胞是,
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“。
上述单元格需要优先选择颜色更改,下面的单元格只需要更改颜色以进行状态更新。我该如何做呢?
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"
答案 0 :(得分:3)
您可以将您输入的内容直接放在range
语句中。如上所述,由于vba在范围语句中限制为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"))
然后,您将使用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
此外,正如@matthewD在评论中所述,您可以命名两个范围并改为引用该名称。如果您多次引用这两个特定范围,这将具有巨大的优势。如果只有一次它是相同的。
由于这已经改变以反映其他人的意见,我已将其更改为社区维基。
答案 1 :(得分:2)
VBA中的字符串常量不能超过255个字符。你必须将声明分开:
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
...
或者在一个陈述中
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"))
因为Union()
采用任意数量的参数。