"对于"布尔变量的循环?

时间:2015-11-26 12:26:05

标签: vba

我有两个布尔变量,即" a"和" b"。

一般来说,变量有四种状态(TT,TF,FT,FF)。

我想看看如何自动将变量从true更改为false以及每个州的代码。

类似于" For"循环,但对于布尔变量。

For a = True to False

    For b = True to False

    Next
Next

4 个答案:

答案 0 :(得分:2)

您只需将布尔值表示为1或0,例如:

Dim b1 As Byte
Dim b2 As Byte
Dim a As Boolean
Dim b As Boolean

For b1 = 0 To 1
    For b2 = 0 To 1
        a = CBool(b1)
        b = CBool(b2)
    Next
Next

答案 1 :(得分:1)

你只需要一个循环:

Dim a As Boolean
Dim b As Boolean
Dim i As Integer

For i = 0 To 3
    a = i And 1
    b = i And 2
    ...
Next

答案 2 :(得分:1)

这是我发现的VBA最佳解决方案,可以保持与原始代码的一致性:

item name     color       size      weight
cup           brown       small     light
shirt         green       medium    light
candy         pink        large     light
chair         tan         medium    light
paper         white       large     light

答案 3 :(得分:0)

这:

Do
    a = CBool(Not a)
    b = CBool(IIf(a, Not b, b))
Loop Until Not a And Not b

将产生所有4个结果,然后退出