2012-01-16 1996 views
8

我爲一個文件創建了一個宏,首先它工作正常,但今天我一直打開並重新啓動文件和宏數百次,我總是得到出現以下錯誤:Excel VBA運行時錯誤'13'類型不匹配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. 非常感謝

回答

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值在下一次迭代中可能不包含其預期值。

+0

這是百達數值介於0和3 – Diogo 2012-01-17 00:29:47

+0

我在代碼的第一行,你給我的錯誤「編譯錯誤:語法錯誤」 – Diogo 2012-01-17 00:38:38

+0

應該不會出現任何語法錯誤。確保你把它放在你的另一個'If'語句之後,並且你有所有的括號。 – 2012-01-17 00:47:08

1

迪奧戈

賈斯汀已經給你一些很細的提示:)

您也將獲得該錯誤如果在您所執行的計算單元具​​有從公式導致的誤差。

例如,如果單元格A1具有#DIV/0!錯誤,那麼你將執行此代碼

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

時,我已經對你的代碼中的一些細微變化得到「Excel的VBA運行時錯誤‘13’類型不匹配」。你能幫我測試一下嗎?將代碼複製到行號中,因爲我故意將它們放在那裏。

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 
+0

我得到了錯誤「編譯錯誤:變量未定義」在130行「lastrow =」 – Diogo 2012-01-16 23:55:44

+0

修正:)請現在嘗試。 – 2012-01-17 06:11:58

+0

沒有錯誤,但我的excel表單凍結,每次我嘗試它:( – Diogo 2012-01-17 13:12:39

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 
0

我有和上面提到的一樣的問題,我的代碼昨天整天都很棒。

我一直在編程今天早上,當我打開我的應用程序(我的文件與Auto_Open子),我得到了運行時錯誤'13'類型不匹配,我去網上找到答案,我試了一個很多事情和修改,並且我曾經記得,即使我們沒有看到它,我也會在某處讀到關於「Ghost」數據的信息。

我的代碼只從一個文件轉移到另一個文件,然後求和。我的代碼在第三個SheetTab中停下來(因此,它的右邊是之前的兩個SheetTab,其中相同的代碼不會停下來)與類型不匹配消息。當我重新啓動我的代碼時,它每次都在同一個SheetTab中執行此操作。因此,我選擇了它停止的單元格,手動輸入0,00(因爲類型不匹配來自在DIM中聲明的Summation變量爲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 
0

當輸入變量類型錯誤時會發生此錯誤。你可能已經寫了Cells(4 + i, 57)的公式,而不是=0,使用公式= ""。所以當運行這個錯誤時顯示。因爲空字符串不等於零。

enter image description here