Excelmacros – select案例

我没有任何人发布这个问题,如果我错了,不要开枪。 :P

我有一个怪物,如果其他结构约300-400线。 有了一个案例结构,我可以把它削减到100或更less。 麻烦的是,你只能find固定值,而不是单元值的select案例。

代码是做什么的?

可以给C17-C22一个值,即1或0.如果该值为1,则将文本复制到工作表中,如果该值为0则什么都不会发生。

Select case ??? Case "C17" = 1 show me text!! Case "C18" = 1 show me text!! ... End select 

我知道我可以做不同的select案例

 Select case "C17".value Case 1 show me text!! End select ... 

但我认为应该可以在一个BIG Select Case中完成,而不是多个Select Case。

非常感谢!

这听起来像是你试图把你的问题搞成一个Select Case结构让自己的生活变得更加复杂。 顺便说一句, Select CaseIf Else 几乎完全一样; 区别主要是化妆品,你原则上可以互换使用 。

为什么不是这样的:

 Dim i As Long Dim varFlags As Variant Dim varText As Variant ' Load flags from sheet. varFlags = Range("C17:C22") ' Load text associated with each flag. Here I'll just hardcode them. varText = Array("Text17", "Text18", "Text19", "Text20", "Text21", "Text22") For i = LBound(varFlags, 1) To UBound(varFlags, 1) If varFlags(i, 1) = 1 Then 'It's a 1. Copy the relevant text. 'Here I just MsgBox it. MsgBox varText(i - 1) 'Sorry for the i-1, my varText array is zero-based! Else 'It's not a 1. Do nothing. End If Next i 

select语句是为了区分variables可以有的不同值。 即Select Case是等式的左边,下面的case是等式右边的不同。 我认为你的任务不是Select case的工作

如果每个细胞的情况都是一样的,循环遍历你的范围,并为每个细胞做select。 但是,如果你只检查单元格是0还是1,那么使用if

就像是:

 For each c in SourceRange if c.value Then ... end if Next c