VBA运行时错误13:types不匹配

我的macros是一个非常简单的replace程序,它识别总体语句中缩短的部分,并用完整的语句replace它们。 出于某种原因,我不断得到VBA错误13:types不匹配。 debugging器在我的replace语句的第一个上识别出这个错误,然后在程序运行这些行时甚至产生预期的结果(我通过注释掉特定的行来testing),在接下来的两行中没有识别错误。 debugging器然后在这两行之后的其余语句中find一个错误。 我不知道发生了什么,将不胜感激任何帮助。 提前致谢。

// // 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 

您的代码很长,如果没有完整的代码,很难确定问题。 首先,我会尝试窃取你的代码。 看看下面,看看是否有帮助。 数组被使用。 如果您不熟悉它们,请在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