2012-01-16 8 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

난 당신이 내게 준 코드의 첫 번째 줄에 오류가 발생했습니다 – Diogo

+0

0과 3 그것은에 allways 숫자 값의 "컴파일 오류 : 구문 오류가" – Diogo

+0

어떤 구문 오류가 없어야합니다. 다른'If' 문 바로 뒤에 넣고 모든 괄호를 사용했는지 확인하십시오. –

1

디오

저스틴 당신에게 아주 좋은 팁 :

당신이 계산을 수행하는 셀에 수식의 결과에 오류가있는 경우 당신은 또한 오류가 발생합니다을 부여하고있다.

예를 들어 셀 A1에 # DIV/0! 나는 당신의 코드에 약간의 변경 한이 코드

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

을 수행 할 때 오류가 당신은 "엑셀 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

+0

Amended : 줄에서 "컴파일 오류 : 변수가 정의되지 않았습니다"오류가 발생했습니다. 지금 시도하십시오. –

+0

오류는 없지만 엑셀 시트를 사용할 때마다 얼어 붙습니다. ( – Diogo

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에서 멈 췄기 때문에 (형식이 일치하지 않는 메시지와 함께 동일한 코드가 멈추지 않은 2 개의 이전 SheetTab에 맞았습니다) 그리고 코드를 다시 시작할 때마다 동일한 SheetTab에서 매번이를 수행합니다.

그래서 내가 멈 췄던 셀을 수동으로 입력했는데, 0,00 (타입 불일치가 DIM에서 Double로 선언 된 Summation 변수에서 나오기 때문에), 동일한 문제가 발생한 모든 후속 셀에 해당 셀을 복사했습니다. 그것은 문제를 해결했습니다. 다시는 메시지를 전하지 않았습니다. 내 코드가 아니라 "유령"또는 과거의 데이터와 관련이 없습니다. 그것은 당신이 Control + End를 사용할 때와 같으며 엑셀은 당신이 한 번 데이터를 가지고 그것을 지웠을 때 당신을 데려 간다. 엑셀이 올바른 셀을 가리키고 있는지 확인하기 위해 Control + End를 사용할 때 "저장"하고 파일을 닫아야합니다.

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 대신 = "" 수식을 사용했을 가능성이 큽니다. 따라서이 오류를 실행하면 표시됩니다. 빈 문자열이 0이 아니기 때문입니다.

enter image description here

관련 문제