【问题标题】:Type mismatch error in excel VBAexcel VBA中的类型不匹配错误
【发布时间】:2014-01-05 05:17:58
【问题描述】:

需要一些建议,当我尝试运行此宏时收到此错误,我收到不匹配错误。

“InsertNAtoBlanks”是不言自明的,我只需要这个 IF 语句来读取该表中的特定列,如果任何单元格中有空格,则运行“InsertNAtoBlanks”宏,如果没有,则继续下一个进入宏。

Dim x As ListObject
Dim z As Range
Dim Bcell As Range

Set x = ActiveSheet.ListObjects("Table6")
Set z = x.DataBodyRange.Columns(11)

For Each Bcell In z
If Bcell.Value <> Empty Then

Run "InsertNAtoBlanks"

 Else

'Contiue on with macro

【问题讨论】:

    标签: excel if-statement range type-mismatch vba


    【解决方案1】:

    如果有空格

    改变

    If Bcell.Value <> Empty Then
    

    If Bcell.Value <> "" Then
    

    或者去

    If Len(Trim(Bcell.Value)) <> 0 Then
    

    Empty 关键字用作Variant 子类型。它表示一个未初始化的变量值。

    你的代码可以写成

    Sub Sample()
        Dim x As ListObject
        Dim z As Range
        Dim Bcell As Range
    
        Set x = ActiveSheet.ListObjects("Table6")
        Set z = x.DataBodyRange.Columns(11)
    
        For Each Bcell In z.Cells
            If Bcell.Value <> "" Then
                Run "InsertNAtoBlanks"
            End If
        Next
    End Sub
    

    【讨论】:

    • Siddaharth,我都试过了,但仍然出现运行时错误“13”:类型不匹配
    • 查看更新后的帖子。您可能需要刷新页面
    • 我使用了提供的代码,我没有像以前那样得到类型不匹配错误。所以这是个好消息,当我运行宏时,我收到错误“未找到单元格”。我在代码中添加了 else ,我仍然得到同样的错误。
    • i got the error "no cells were found" 你在哪一行得到了那个错误?
    • 您在任何时候都不会得到错误或超过一行。那么你在哪一行得到错误? For Each Bcell In z.CellsIf Bcell.Value &lt;&gt; "" ThenRun "InsertNAtoBlanks"
    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多