我还没有人发布这个问题,如果我错了,请不要拍我! :P
如果其他结构,我有一个大约 300-400 行的怪物。使用案例结构,我可以将其减少到 100 或更少。麻烦的是您只能找到具有固定值而不是单元格值的选定案例。
代码有什么作用?
C17-C22 可以被赋予一个值,1 或 0。如果值为 1,则将文本复制到工作表中,如果值为 0,则不会发生任何事情。
Select case ???
Case "C17" = 1
show me text!!
Case "C18" = 1
show me text!!
...
End select
我知道我可以用不同的选择案例来做到这一点
Select case "C17".value
Case 1
show me text!!
End select
...
但我认为应该可以在一个 BIG Select Case 而不是多个 Select Case 中做到这一点。
非常感谢!
最佳答案
听起来你试图将你的问题硬塞到 Select Case
中,这让你自己的生活变得更加复杂。构造。 Select Case
,顺便说一下,几乎与 If Else
完全相同。 ;区别主要在于外观,原则上您可以use them interchangeably .
为什么不这样:
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 - Excel 宏 - 选择案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5854532/