【问题标题】:Excel VBA Run-time error '13' Type mismatchExcel VBA 运行时错误“13”类型不匹配
【发布时间】:2012-01-16 19:52:19
【问题描述】:

我为一个文件创建了一个宏,首先它工作正常,但今天我已经打开并重新启动文件和宏数百次,我总是收到以下错误:

Excel VBA 运行时错误“13”类型不匹配

我没有更改宏中的任何内容,也不知道为什么会出现错误。此外,每次我运行宏都需要很长时间来更新宏(宏必须运行大约 9000 行)。

错误在** **之间的那一行。

VBA:

Sub k()

Dim x As Integer, i As Integer, a As Integer
Dim name As String
name = InputBox("Please insert the name of the sheet")
i = 1
Sheets(name).Cells(4, 58) = Sheets(name).Cells(4, 57)
x = Sheets(name).Cells(4, 57).Value
Do While Not IsEmpty(Sheets(name).Cells(i + 4, 57))
    a = 0
    If Sheets(name).Cells(4 + i, 57) <> x Then
        If Sheets(name).Cells(4 + i, 57) <> 0 Then
            If Sheets(name).Cells(4 + i, 57) = 3 Then
                a = x
                Sheets(name).Cells(4 + i, 58) = Sheets(name).Cells(4 + i, 57) - x
                x = Cells(4 + i, 57) - x
            End If
            **Sheets(name).Cells(4 + i, 58) = Sheets(name).Cells(4 + i, 57) - a**
            x = Sheets(name).Cells(4 + i, 57) - a
        Else
        Cells(4 + i, 58) = ""
        End If
    Else
    Cells(4 + i, 58) = ""
    End If

i = i + 1
Loop

End Sub

我在 Windows 7 上使用 excel 2010。

【问题讨论】:

  • 可能您正在执行 if 语句并比较字符串而不使用双引号。使用="3" 而不是= 3

标签: excel excel-2010 vba


【解决方案1】:

如果Sheets(name).Cells(4 + i, 57) 包含非数字值,您将得到类型不匹配。您应该在假设它们是数字之前验证这些字段并尝试从中减去。

此外,您应该启用Option Strict,以便在尝试对变量执行类型相关的操作(例如减法)之前强制显式转换变量。这也将帮助您识别和消除未来的问题。 不幸的是,Option Strict 仅适用于 VB.NET。不过,您应该查找 VBA 中显式数据类型转换的最佳实践。


更新:

但是,如果您想快速修复您的代码,请将** 行和它后面的行包含在以下条件中:

If IsNumeric(Sheets(name).Cells(4 + i, 57))
    Sheets(name).Cells(4 + i, 58) = Sheets(name).Cells(4 + i, 57) - a
    x = Sheets(name).Cells(4 + i, 57) - a
End If

但请注意,您的 x 值可能不包含其在下一次迭代中的预期值。

【讨论】:

  • 它总是介于 0 和 3 之间的数值。
  • 我在你给我的代码的第一行出现错误“编译错误:语法错误”
  • 不应该有任何语法错误。确保你把它放在你的另一个 If 声明之后并且你有所有的括号。
  • Option Strict 是 VB.NET,而不是 VBA。
  • Option Strict 在 VBA for Excel 中不可用,但是,您可以在以下 4 种之间进行选择:Base、Compare、Explicit 和 Private。
【解决方案2】:

感谢大家的帮助!多亏了一个朋友和你,我终于能够让它完美地工作! 这是最终代码,因此您也可以看到我们如何解决它。

再次感谢!

Option Explicit

Sub k()

Dim x As Integer, i As Integer, a As Integer
Dim name As String
'name = InputBox("Please insert the name of the sheet")
i = 1
name = "Reserva"
Sheets(name).Cells(4, 57) = Sheets(name).Cells(4, 56)

On Error GoTo fim
x = Sheets(name).Cells(4, 56).Value
Application.Calculation = xlCalculationManual
Do While Not IsEmpty(Sheets(name).Cells(i + 4, 56))
    a = 0
    If Sheets(name).Cells(4 + i, 56) <> x Then
        If Sheets(name).Cells(4 + i, 56) <> 0 Then
            If Sheets(name).Cells(4 + i, 56) = 3 Then
                a = x
                Sheets(name).Cells(4 + i, 57) = Sheets(name).Cells(4 + i, 56) - x
                x = Cells(4 + i, 56) - x
            End If
            Sheets(name).Cells(4 + i, 57) = Sheets(name).Cells(4 + i, 56) - a
            x = Sheets(name).Cells(4 + i, 56) - a
        Else
        Cells(4 + i, 57) = ""
        End If
    Else
    Cells(4 + i, 57) = ""
    End If

i = i + 1
Loop
Application.Calculation = xlCalculationAutomatic
Exit Sub
fim:
MsgBox Err.Description
Application.Calculation = xlCalculationAutomatic
End Sub

【讨论】:

  • 您提供了解决方案,但没有解释发生了什么变化...
【解决方案3】:

迪奥戈

贾斯汀给了你一些非常好的建议:)

如果您执行计算的单元格因公式而出现错误,您也会收到该错误。

例如,如果单元格 A1 有 #DIV/0!错误,则执行此代码时您将收到“Excel VBA 运行时错误'13'类型不匹配”

Sheets("Sheet1").Range("A1").Value - 1

我对您的代码做了一些细微的改动。你能帮我测试一下吗?复制带有行号的代码,因为我故意将它们放在那里。

Option Explicit

Sub Sample()
  Dim ws As Worksheet
  Dim x As Integer, i As Integer, a As Integer, y As Integer
  Dim name As String
  Dim lastRow As Long
10        On Error GoTo Whoa

20        Application.ScreenUpdating = False

30        name = InputBox("Please insert the name of the sheet")

40        If Len(Trim(name)) = 0 Then Exit Sub

50        Set ws = Sheets(name)

60        With ws
70            If Not IsError(.Range("BE4").Value) Then
80                x = Val(.Range("BE4").Value)
90            Else
100               MsgBox "Please check the value of cell BE4. It seems to have an error"
110               GoTo LetsContinue
120           End If

130           .Range("BF4").Value = x

140           lastRow = .Range("BE" & Rows.Count).End(xlUp).Row

150           For i = 5 To lastRow
160               If IsError(.Range("BE" & i)) Then
170                   MsgBox "Please check the value of cell BE" & i & ". It seems to have an error"
180                   GoTo LetsContinue
190               End If

200               a = 0: y = Val(.Range("BE" & i))
210               If y <> x Then
220                   If y <> 0 Then
230                       If y = 3 Then
240                           a = x
250                           .Range("BF" & i) = Val(.Range("BE" & i)) - x

260                           x = Val(.Range("BE" & i)) - x
270                       End If
280                       .Range("BF" & i) = Val(.Range("BE" & i)) - a
290                       x = Val(.Range("BE" & i)) - a
300                   Else
310                       .Range("BF" & i).ClearContents
320                   End If
330               Else
340                   .Range("BF" & i).ClearContents
350               End If
360           Next i
370       End With

LetsContinue:
380       Application.ScreenUpdating = True
390       Exit Sub
Whoa:
400       MsgBox "Error Description :" & Err.Description & vbNewLine & _
         "Error at line     : " & Erl
410       Resume LetsContinue
End Sub

【讨论】:

  • 我在第 130 行 "lastrow=" 收到错误 "编译错误:变量未定义"
  • 没有错误,但每次我尝试时我的 Excel 表都会冻结:(
  • 既然有 9000 行,就给它一些时间。等着看会发生什么。或者,如果您想将第 20 行更改为 True 而不是 False。 Application.ScreenUpdating = True 然后检查它。它是否显示出任何活着的迹象?
【解决方案4】:

对于未来的读者:

这个函数在Run-time error '13': Type mismatch中异常结束

Function fnIsNumber(Value) As Boolean
  fnIsNumber = Evaluate("ISNUMBER(0+""" & Value & """)")
End Function

在我的例子中,函数在遇到 #DIV/0!N/A 值时失败。

为了解决这个问题,我必须这样做:

Function fnIsNumber(Value) As Boolean
   If CStr(Value) = "Error 2007" Then '<===== This is the important line
      fnIsNumber = False
   Else
      fnIsNumber = Evaluate("ISNUMBER(0+""" & Value & """)")
   End If
End Function

【讨论】:

    【解决方案5】:
    Sub HighlightSpecificValue()
    
    'PURPOSE: Highlight all cells containing a specified values
    
    
    Dim fnd As String, FirstFound As String
    Dim FoundCell As Range, rng As Range
    Dim myRange As Range, LastCell As Range
    
    'What value do you want to find?
      fnd = InputBox("I want to hightlight cells containing...", "Highlight")
    
        'End Macro if Cancel Button is Clicked or no Text is Entered
          If fnd = vbNullString Then Exit Sub
    
    Set myRange = ActiveSheet.UsedRange
    Set LastCell = myRange.Cells(myRange.Cells.Count)
    
    enter code here
    Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
    
    'Test to see if anything was found
      If Not FoundCell Is Nothing Then
        FirstFound = FoundCell.Address
    
      Else
        GoTo NothingFound
      End If
    
    Set rng = FoundCell
    
    'Loop until cycled through all unique finds
      Do Until FoundCell Is Nothing
        'Find next cell with fnd value
          Set FoundCell = myRange.FindNext(after:=FoundCell)
    
    
    
    
    
    
    
        'Add found cell to rng range variable
          Set rng = Union(rng, FoundCell)
    
        'Test to see if cycled through to first found cell
          If FoundCell.Address = FirstFound Then Exit Do
    
    
      Loop
    
    'Highlight Found cells yellow
    
      rng.Interior.Color = RGB(255, 255, 0)
    
      Dim fnd1 As String
      fnd1 = "Rah"
      'Condition highlighting
    
      Set FoundCell = myRange.FindNext(after:=FoundCell)
    
    
    
      If FoundCell.Value("rah") Then
          rng.Interior.Color = RGB(255, 0, 0)
    
      ElseIf FoundCell.Value("Nav") Then
    
        rng.Interior.Color = RGB(0, 0, 255)
    
    
    
        End If
    
    
    
    
    
    'Report Out Message
      MsgBox rng.Cells.Count & " cell(s) were found containing: " & fnd
    
    Exit Sub
    
    'Error Handler
    NothingFound:
      MsgBox "No cells containing: " & fnd & " were found in this worksheet"
    
    End Sub
    

    【讨论】:

      【解决方案6】:

      我遇到了和你上面提到的同样的问题,我的代码昨天一整天都做得很好。

      今天早上我继续编程,当我打开我的应用程序(我的带有 Auto_Open 子文件的文件)时,我得到了运行时错误“13”类型不匹配,我上网寻找答案,我尝试了很多的东西,修改,有一次我记得我在某处读到过“幽灵”数据,即使我们看不到它也会留在单元格中。

      我的代码仅将数据从我之前打开的一个文件传输到另一个文件并求和。我的代码在第三个 SheetTab 处停止(所以它在前面的 2 个 SheetTab 中运行,其中相同的代码没有停止),并带有类型不匹配消息。当我重新启动我的代码时,它每次都会在同一个 SheetTab 上这样做。

      所以我选择了它停止的单元格,手动输入 0,00(因为类型不匹配来自在 DIM 中声明为 Double 的 Summation 变量)并将该单元格复制到发生相同问题的所有后续单元格中。它解决了这个问题。再也没有消息了。与我的代码无关,但与“幽灵”或过去的数据无关。就像当您想使用 Control+End 时,Excel 会将您带到您曾经拥有数据的地方并删除它。当您想使用 Control+End 确保 Excel 将您指向正确的单元格时,必须“保存”并关闭文件。

      【讨论】:

        【解决方案7】:

        当输入变量类型错误时会发生此错误。您可能已经在 Cells(4 + i, 57) 中编写了一个公式,而不是 =0,而是使用了公式 = ""。所以运行的时候会显示这个错误。因为空字符串不等于零。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多