我为一个文件创建了一个宏,并且它首先工作正常,但今天我一直在打开并重启文件和宏数百次,我总是收到以下错误: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。
非常感谢
答案 0 :(得分:9)
如果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
值可能不会在下一次迭代中包含其预期值。
答案 1 :(得分: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
答案 2 :(得分:1)
迪奥戈
贾斯汀给了你一些非常好的提示:)
如果您执行计算的单元格因公式而产生错误,您也会收到该错误。
例如,如果Cell 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
答案 3 :(得分:1)
对于将来的读者:
此功能在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
答案 4 :(得分:0)
我遇到了与上面提到的相同的问题,我的代码昨天整天都很棒。
我今天早上继续编程,当我打开我的应用程序(我的文件带有Auto_Open子)时,我得到运行时错误'13'类型不匹配,我上网找到答案,我试了很多事情,修改,有一点我记得我读过一些关于“Ghost”数据的内容,即使我们没有看到它也会留在单元格中。
我的代码只执行从先前打开的一个文件到另一个文件的数据传输并将其汇总。我的代码在第三个SheetTab停止了(所以它适用于之前的2个SheetTab,其中相同的代码没有停止),类型不匹配消息。当我重新启动代码时,它每次都在同一个SheetTab上执行此操作。
所以我选择停止的单元格,手动输入0,00(因为类型不匹配来自DIM中声明为Double的Summation变量),并在发生相同问题的所有后续单元格中复制该单元格。它解决了这个问题。再也没有留言了。与我的代码无关,而是“Ghost”或过去的数据。就像你想要使用Control + End一样,Excel会将你带到数据的位置并将其删除。当您想使用Control + End确保Excel指向正确的单元格时,必须“保存”并关闭文件。
答案 5 :(得分: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
答案 6 :(得分:0)