确定多个单元格的 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:$C")) 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"
您可以将键入的内容直接放在 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 在评论中所述,您可以命名两个范围并引用名称。如果您将多次引用这两个特定范围,这将具有巨大的优势。如果只有一次就一样。
由于已更改以反映其他人的意见,我已将其更改为社区 wiki。
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"))
as Union()
接受任意数量的参数。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("$C:$C")) 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"
您可以将键入的内容直接放在 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 在评论中所述,您可以命名两个范围并引用名称。如果您将多次引用这两个特定范围,这将具有巨大的优势。如果只有一次就一样。
由于已更改以反映其他人的意见,我已将其更改为社区 wiki。
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"))
as Union()
接受任意数量的参数。