簡體   English   中英

VBA Excel 運行時錯誤 13 類型不匹配

[英]VBA Excel run time error 13 type mismatch

我有輸入框的問題。 我的宏運行良好。 這是我的代碼,所以我在谷歌上發現,如果 CInt 函數提供了一個無法轉換為數值的文本字符串,它將返回錯誤,這是真的嗎? 或任何建議?

Private Sub btn_check_Click()
Application.ScreenUpdating = False

Dim a, b, x, y, z, c As Long
Dim indicator, puzzlePartner, puzzleRaw As String

a = 5
b = 2
c = 5

Do Until Cells(c, 1) = ""
     Sheet9.Cells(c, 3).Select
        Sheet9.Cells(c, 56) = Trim(CStr(Left(Sheet9.Cells(c, 3), 15)))
    c = c + 1
    Loop

a = 5

Do Until Sheet9.Cells(a, 1) = ""
    Do Until Sheet4.Cells(b, 104) = ""
        If a = 2000 Then
        c = 1
        End If
    If Sheet4.Cells(b, 9) = Sheet9.Cells(a, 9) And Sheet4.Cells(b, 27) = Sheet9.Cells(a, 27) And Sheet4.Cells(b, 26) = Sheet9.Cells(a, 26) And _
         Sheet4.Cells(b, 25) = Sheet9.Cells(a, 25) And Sheet4.Cells(b, 24) = Sheet9.Cells(a, 24) And Sheet9.Cells(a, 52) = Sheet4.Cells(b, 104) And _
         LCase(Trim(Sheet4.Cells(b, 21))) = LCase(Trim(Sheet9.Cells(a, 21))) Then
         Sheet9.Cells(a, 53) = "True"
        GoTo 1
    ElseIf CInt(Cells(2, 3)) <> Year(Sheet9.Cells(a, 9)) And Sheet9.Cells(a, 15) = "Withdrawn" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
    ElseIf CInt(Cells(2, 3)) <> Year(Sheet9.Cells(a, 9)) And Sheet9.Cells(a, 15) = "DC" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
    ElseIf CInt(Cells(2, 3)) <> Year(Sheet9.Cells(a, 9)) And Sheet9.Cells(a, 15) = "PO" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
    ElseIf Right(Sheet9.Cells(a, 7), 3) = "(S)" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
     ElseIf CInt(Cells(2, 3)) <> Year(Sheet9.Cells(a, 51)) And Sheet9.Cells(a, 15) = "APP" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
    ElseIf CInt(Cells(2, 3)) <> Year(Cells(a, 12)) And Right(Cells(a, 52), 3) = "RNW" Then
        Sheet9.Cells(a, 53) = "True"
        GoTo 1
        End If
    End If
2:
    b = b + 1
    Loop
            Sheet9.Cells(a, 53) = "False"
x = CInt(Len(Sheet9.Cells(a, 52)))
            y = x - 4
            indicator = Mid(Sheet9.Cells(a, 52), y, 1)
            If Sheet9.Cells(a, 53) = "False" And indicator = "-" Then
                Sheet9.Cells(a, 53) = "Top-up"
          End If
1:

    b = 2
a = a + 1
Loop

我也注意到這個錯誤顯示在行上

     ElseIf CInt(Cells(2, 3)) <> Year(Sheet9.Cells(a, 51)) And Sheet9.Cells(a, 15) = "APP" Then

任何人都知道為什么會這樣?

是的, CInt方法在傳遞非數值時返回運行時錯誤。 您可以使用

IsNumeric(SheetName.Cells(2, 3))

在使用CInt之前執行類型檢查的方法。 IsNumeric將根據傳遞的輸入類型返回truefalse

更新

如果是日期類型,您可以使用IsDate()方法來驗證單元格值是否為日期類型。 只有在類型檢查后使用Year()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM