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

11

我为一个文件创建了一个宏,一开始它运行良好,但今天我已经打开和重新启动文件和宏数百次,每次都会出现以下错误:

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 - csandreas1
7个回答

10
如果Sheets(name).Cells(4 + i, 57)中包含非数字值,将会出现类型不匹配的错误。在尝试对其进行减法运算之前,应该验证这些字段是否为数字。 更新: 如果你想快速修复代码,可以将**这一行和下一行包裹在以下条件语句中:
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之间的数字值。 - Diogo
我在你给我的代码的第一行遇到了错误:“编译错误:语法错误”。 - Diogo
不应该有任何语法错误。确保将其放在其他“if”语句之后,并且您拥有所有括号。 - Devin Burke
1
Option Strict 是 VB.NET 中的语法,不是 VBA。 - phoog
在Excel的VBA中,没有“Option Strict”选项,但是您可以在以下四个选项之间进行选择:Base、Compare、Explicit和Private。 - James Heffer

4
感谢大家的帮助!最终我通过一个朋友和你们的帮助成功运行了它!以下是最终代码,您可以看到我们是如何解决它的。
再次感谢!
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

1
你提供了一个解决方案,但你没有解释发生了什么变化... - LeanMan

1

针对未来读者:

该函数出现了运行时错误 '13':类型不匹配

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

1

Diogo

Justin 给了你一些非常好的提示 :)

如果您执行计算的单元格由于公式而产生错误,您也会收到该错误。

例如,如果单元格 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=”这一行。 - Diogo
没有错误,但每次尝试后我的Excel表格都会冻结 :( - Diogo
由于有9000行,需要一些时间。等待并观察会发生什么。或者,如果您想要更改第20行为True而不是False,则可以这样做。Application.ScreenUpdating = True,然后检查它。它是否显示任何活动迹象? - Siddharth Rout

0

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

enter image description here


0

我遇到了你在这里提到的同样的问题,而且昨天我的代码一整天都很好。

今天早上我继续编程,当我打开我的应用程序(我的文件中有一个Auto_Open子)时,出现了“运行时错误'13'类型不匹配”的提示。我上网寻找答案,尝试了很多事情和修改,最后我想起我曾经在某个地方读到过即使我们看不见,也会留在单元格中的“幽灵”数据。

我的代码只是从我之前打开的一个文件向另一个文件进行数据传输并求和。我的代码在第三个SheetTab处停止了(所以它在两个先前的SheetTab中运行得很好,而且没有停止),并出现了类型不匹配的消息。每当我重新启动我的代码时,它都会在相同的SheetTab处发生。

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


0
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接