定义变量时不允许使用指数?

时间:2015-12-13 19:28:11

标签: excel vba excel-vba

我有简单的VBA程序,可以创建真值表。这些行是红色的(某种语法错误):

 end0=2^power/2

end1=2^power

这里是他们的背景:

Sub filltruthtable()
cases = 2 * inputnum
Dim power
power = inputnum - colcounter
Dim end0
end0=2^power/2
Dim start1
start1 = end0 + 1
Dim end1
end1=2^power
For counterrow = 1 To inputnum
    For counterrow = 1 To end0
        Cells(counterrow, countercol).Value = 0
    Next
    For counterrow = start1 To end1
        Cells(counterrow, countercol).Value = 0
    Next
Next
End Sub

1 个答案:

答案 0 :(得分:0)

在定义变量时允许使用Exponent,但您必须确保它实际上是一个数字,而不是包含数字的字符串。

我已经将filltruthtable()中使用的所有变量声明为长整数,但这只是因为我不知道您将使用哪种数字,或者您是否已使用公共或模块范围声明变量。 / p>

当给变量赋值时,我总是会确保该值符合我想要的类型,例如

Sub Demo()
    Dim Demo_Num As Integer
    Demo_Num = CInt(Application.InputBox ("Please give an input.", "Input")
end sub

现在Demo_Num是一个长整数,可以用作指数。

Sub filltruthtable()

    Dim cases As Long, Dim inputnum As Long, Dim power As Long, 
    Dim end0 As Long, Dim start1 As Long, Dim end1 As Long, 
    Dim colcounter As Long, Dim counterrow As Long, Dim countercol As Long

    cases = 2 * inputnum
    power = inputnum - colcounter
    end0 = 2 ^ power / 2
    start1 = end0 + 1
    end1 = 2 ^ power

    For counterrow = 1 To inputnum
        For counterrow = 1 To end0
            Cells(counterrow, countercol).Value = 0
        Next
        For counterrow = start1 To end1
            Cells(counterrow, countercol).Value = 0
        Next
    Next
End Sub