if 1 = "1" then
    '1_这个if是true Q1:为什么类型不一样,确相等?
end ifDim i, j
i = "1"
j = 1
if j = "1" then
    '2_这个if是true Q2:这里和Q1
end ifif i = j then
    '3_这个if是false Q3:这里就更加BT了,如果是两个变量其就得出想要的结果。
end if 
希望高手能解答。VBA

解决方案 »

  1.   

    这里,VB 表达式进行了自动类型转换。?1 = "1"
    ?1 = " 1"
    ?1 = " 1 "均得到 True;?1 = "2"得到 False;?1 = " 1 2"得到类型不匹配错误。因为无法中此字符串转换成 Integer。如果 Dim i As String, j As Integer,则后面的表达式均为 True。这与类型自动转换规则有关。但作为好的编程习惯,不建议写这样的代码。
      

  2.   

    这个跟Dim定义变量有关系,VBA默认的数据类型为Variant,在进行数学运算时,会自动转型为数值型;在进行两个字符连接时,就转换成字符型。
    例如:Dim SomeValue      '缺省为 Variant。
    SomeValue = "17"   'SomeValue包含 "17"(双字符的串)。
    SomeValue = SomeValue - 15       '现在, SomeValue 包含数值 2。
    SomeValue = "U" & SomeValue   '现在, SomeValue 包含 "U2" (双字符的串)。