如何在visual basic中评估条件中值的类型名称?

时间:2018-01-05 19:28:26

标签: excel-vba vba excel

我使用visual basic为excel编写宏。我已经查找了几个教程,所有这些都使用"如果TypeOf [object]是[typename]那么"为了检查单元格的值是否属于某种类型。例如,在我的代码中,我尝试评估单元格A1中的值是否为字符串以便移动它。我的代码:

Private Sub CommandButton1_Click()

 If TypeOf Range("A1").Value Is String Then

  Range("B1").Value = Range("A1").Value 

 End If 

End Sub 

然而," String"突出显示,我收到一条错误消息:编译错误: 预期:对象或类型名称

我已经被困住了很长一段时间,根本无法找到或寻找出路。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以使用git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -r git branch -d;

TypeName()

答案 1 :(得分:0)

假设有一个Worker类:

Sub F()

    Dim x As Integer
    Dim j As Long
    Dim w As Object

    Set w = New Worker

    MsgBox TypeName(w) '// Worker
    MsgBox VarType(w)  '// 9 = vbObject

    If TypeName(x) = "Integer" Then
    End If

    If VarType(x) = vbInteger Then
    End If

    If VarType(j) = vbLong Then
    End If

End Sub