【问题标题】:VBA Run Time Error 13: Type MismatchVBA 运行时错误 13:类型不匹配
【发布时间】:2013-07-12 15:36:44
【问题描述】:

我的宏是一个非常简单的替换程序,它识别整个语句的缩短部分并将它们替换为完整语句。出于某种原因,我不断收到 VBA 错误 13:类型不匹配。调试器在我的第一条替换语句中识别出这个错误,然后在接下来的两行中没有识别出错误,当程序运行这些行时甚至会产生预期的结果(我通过注释掉特定行进行了测试)。然后,调试器会在这两行之后的其余语句中发现错误。我不知道发生了什么,非常感谢任何帮助。 提前致谢。

//

//

Sub Replacement()

    **'Define variables**

    Dim firstViolation As String

    Dim secondViolation As String

    Dim thirdViolation As String

    Dim fourthViolation As String

    Dim fifthViolation As String

    Dim sixthViolation As String

    Dim seventhViolation As String

    Dim eighthViolation As String

    Dim ninthViolation As String

    Dim tenthViolation As String

    Dim eleventhViolation As String

    Dim twelfthViolation As String

    Dim thirteenthViolation As String

    Dim fourteenthViolation As String


    **'Give variables values**

    These statements are incredibly long but I am confident there is no error as I simply set the pre-defined variables


    **'Code to replace the word violation with the correct descriptions**

    Range("G2:G100").Replace What:="IPMC-301.4 Emergency Phone Contact", Replacement:=firstViolation, LookAt:=xlPart

    Range("H2:H100").Replace What:="IPMC-302.3 Sidewalks", Replacement:=secondViolation, LookAt:=xlPart

    Range("I2:I100").Replace What:="IPMC-302.7 Accessory Structures", Replacement:=thirdViolation, LookAt:=xlPart

    Range("J2:J100").Replace What:="IPMC-302.8 Motor vehicles, boats and trailers", Replacement:=fourthViolation, LookAt:=xlPart

    Range("K2:K100").Replace What:="IPMC-302.10 Graffiti Removal", Replacement:=fifthViolation, LookAt:=xlPart

    Range("L2:L100").Replace What:="IPMC-302.13 Parking of motor vehicles",
    Replacement:=sixthViolation, LookAt:=xlPart

    Range("M2:M100").Replace What:="IPMC-304.2 Protective Treatment", Replacement:=seventhViolation, LookAt:=xlPart

    Range("N2:N100").Replace What:="IPMC-304.3 [F] Premises Identification", Replacement:=eighthViolation, LookAt:=xlPart

    Range("O2:O100").Replace What:="IPMC-304.6 Exterior Walls", Replacement:=ninthViolation, LookAt:=xlPart

    Range("P2:P100").Replace What:="IPMC-304.7 Roofs and Drainage", Replacement:=tenthViolation, LookAt:=xlPart

    Range("Q2:Q100").Replace What:="IPMC-304.3.1 Alley Frontage Identification", Replacement:=eleventhViolation, LookAt:=xlPart

    Range("R2:R100").Replace What:="IPMC-307.1  Accumulation of rubbish or garbage", Replacement:=twelfthViolation, LookAt:=xlPart

    Range("S2:S100").Replace What:="IPMC-307.2.3 Container Locks", Replacement:=thirteenthViolation, LookAt:=xlPart

    Range("T2:T100").Replace What:="IPMC-307.3.4 Additional Capacity Requirements", Replacement:=fourteenthViolation, LookAt:=xlPart

【问题讨论】:

  • 查看成功和失败的替换值可能会有所帮助。它为我运行,没有任何东西可以替换和统一化字符串。作为旁注,请为您的 cmets 使用 VBA 注释字符 - 单引号。我使在 VBE 中的复制和测试变得更容易。
  • 你能粘贴你赋值的部分吗?

标签: vba excel runtime-error type-mismatch


【解决方案1】:

您的代码很长,如果没有完整的代码,很难找出其中的问题。 首先,我会尝试简化您的代码。看看下面,看看它是否有帮助。使用数组。如果您不熟悉它们,请在 google 中查找它们。

Sub replace_strings()

Dim ArrayCh() As Variant
Dim ArrayChTo() As Variant
Dim i As Integer

 ArrayCh = Array("IPMC-301.4 Emergency Phone Contact", "IPMC-302.3 Sidewalks") ' strings to change
 ArrayChTo = Array("blah", "blah blah") ' strings to change to


For i = LBound(ArrayCh) To UBound(ArrayCh)
    With ActiveSheet.Column(7 + i) 'G column where you start from in you code
                                    '+i - given in Arrays the frist index is 0
                                    'then it will add 1,2,3... as we loop the array
        .Replace What:=ArrayCh(i), _
        Replacement:=ArrayChTo(i), _
        LookAt:=xlPart, _
        SearchOrder:=xlByColumns, _
        MatchCase:=True
    End With
Next i

End Sub 

【讨论】:

    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多