运行类型错误13 - 类型不匹配

时间:2014-03-26 23:03:35

标签: excel vba excel-vba

我在

处不断收到运行类型错误13 - 类型不匹配

以下一行:

   Set cel = wshS.Columns(1).Find(What:="EMEA\" + wshT.Cells(r, 10 + cCtr).Value, _
   LookAt:=xlWhole, MatchCase:=False)

你知道是什么原因造成的吗?这是什么意思?

以下完整代码:

Sub VDA_Update()

Dim wshT As Worksheet
    Dim wbk As Workbook
    Dim wshS As Worksheet
    Dim r As Long
    Dim m As Long
    Dim cel As Range
    Application.ScreenUpdating = False
    Set wshT = ThisWorkbook.Worksheets("Master")
    On Error Resume Next

    ' Check whether vda.xlsx is already open
    Set wbk = Workbooks("vda.xlsx")
        On Error GoTo 0
        If wbk Is Nothing Then
        ' If not, open it
        Set wbk = Workbooks.Open("C:\Working\vda.xlsx")
    End If

    ' Set worksheet on vda.xlsx
    Set wshS = wbk.Worksheets("pc_list")
    m = wshT.Cells(wshT.Rows.Count, 1).End(xlUp).Row

    ' Loop though cells in column J on main.xlsm
    For r = 1 To m

        For cCtr = 0 To 2

        ' Can we find the value in column A of vda.xlsx?
        Set cel = wshS.Columns(1).Find(What:="EMEA\" + wshT.Cells(r, 10 + cCtr).Value, _
        LookAt:=xlWhole, MatchCase:=False)

        If Not cel Is Nothing Then

            ' If we find a match, then change cell color

            If cel.Offset(0, 1).Value = "True" Then
                wshT.Cells(r, 10 + cCtr).Interior.ColorIndex = 6
                wshT.Cells(r, 43).Value = "Assigned"
            ElseIf cel.Offset(0, 1).Value = "False" Then
                wshT.Cells(r, 10 + cCtr).Interior.ColorIndex = 8
                wshT.Cells(r, 43).Value = "Unassigned"
            End If

            ' If so, enter "Yes" in column M - Comms Sent?
                ' If wshT.Cells(r, 13).Value = "" Then wshT.Cells(r, 13).Value = "Yes"
            ' Enter "Yes" in column O - VDA Deployed?
                If wshT.Cells(r, 15).Value = "" Then wshT.Cells(r, 15).Value = "Yes"
            ' Enter "5.6.200" in column P - VDA Version
                If wshT.Cells(r, 16).Value = "" Then wshT.Cells(r, 16).Value = "5.6.200"
            ' Enter date in column Q - Migration Date
                ' If wshT.Cells(r, 17).Value = "02/01/2014" Then wshT.Cells(r, 17).Value = "03/03/2014"

            End If
        Next
    Next r

    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

在您正在连接的What:=段之间使用&符号(&)而不是加号(+):

Set cel = wshS.Columns(1).Find(What:="EMEA\" & wshT.Cells(r, 10 + cCtr).Value, _
        LookAt:=xlWhole, MatchCase:=False)

如果您的搜索范围wshT.Cells(r, 10 + cCtr)中有一个包含纯数值的单元格值,VBA将尝试添加" EMEA \"数学上到那个数字(而不是连接它)。在这种情况下,类型不匹配将是文本和数字数据类型之一。