运行代码时收到类型不匹配错误

时间:2021-02-04 13:27:47

标签: excel vba

我一直在使用此代码从 Sheets("Data") 代码中获取数据并搜索第一个标题,即 Gross Wage 然后 Gross Label 然后 Gross DD。如果这些标头匹配,则代码将复制值(在 Sheets("Data") 第 7,8 和 9 行中可用),然后通过转置将值粘贴到 Sheets("Final").Range(G11,H11 and I11) 中。

粘贴顺序是Sheets("Final") C 列和 D 列的年份和月份随 D5 日期而变化。并且 Sheets("Data") 行 6 具有粘贴在 Sheets("Final") 中的值的年份和季度。

现在的问题是它给出了类型不匹配错误。有人可以看看这个问题。为什么会出现错误。

[![在此处输入图片描述][1]][1]

非常感谢您的帮助。

Sub input()

Dim x, y, z, i As Long, ii As Long, iii As Long, iv As Long, lRow As Long
    Dim Output, sCohort As String, lCS As Long, lCE As Long, lMnth As Long
    Dim Hdrs, iGp As Integer, iGdb As Integer, iGsb As Integer

    x = Application.Transpose(Sheet1.[e7].Resize(3))
    For i = LBound(x) To UBound(x)
        If Trim(Split(LCase(x(i)), "-")(1)) = "Gross Wage" Then
            iGp = i
        ElseIf Trim(Split(LCase(x(i)), "-")(1)) = "Gross Label" Then
            iGdb = i
        ElseIf Trim(Split(LCase(x(i)), "-")(1)) = "Gross DD" Then
            iGsb = i
        End If
    Next
    
    lRow = Sheet1.Cells(Rows.Count, 5).End(xlUp).Row
    lCS = Application.Match("Row", Sheet1.Rows(6), 0)
    lCE = Application.Match("Run*", Sheet1.Rows(6), 0)
    x = Sheet1.Cells(6, lCS).Resize(lRow - 5, lCE - lCS)
    y = Sheets("Final").[b11].CurrentRegion
    ReDim z(1 To UBound(y, 1), 1 To 3)
    sCohort = Sheet1.Range("B7")
    lMnth = Sheets("Final").[d11]
    
    Select Case lMnth Mod 3
        
        Case Is = 0: ii = 0
        Case Is = 1: ii = 2
        Case Is = 2: ii = 1
    
    End Select
    
    Select Case lMnth
        
        Case Is < 4: iv = 5
        Case Is < 7: iv = 6
        Case Is < 10: iv = 7
        Case Is < 13: iv = 4
    
    End Select
    
    iii = Application.Choose(lMnth, 7, 6, 5, 4, 3, 2, 1, 0, -1, 10, 9, 8)
    
    If sCohort = vbNullString Then Exit Sub
    
    For i = iv To UBound(x, 2)
        
        If x(1, i) Like "Q*" Then
            ii = ii + 3
            z(ii, 1) = x(iGp + 1, i)
            z(ii, 2) = x(iGdb + 1, i)
            z(ii, 3) = x(iGsb + 1, i)
            
        ElseIf x(1, i) Like "Y*" Then
            iii = iii + 12
            z(iii, 1) = x(iGp + 1, i)
            z(iii, 2) = x(iGdb + 1, i)
            z(iii, 3) = x(iGsb + 1, i)
            
        End If
    Next
    
    Sheets("Final").[g10].Resize(UBound(z, 1), UBound(z, 2)) = z
        
End Sub

1 个答案:

答案 0 :(得分:1)

你可以这样处理错误:

If Not IsError(Application.match("Row", Sheet1.Rows(6), 0)) Then
    lCS = Application.match("Row", Sheet1.Rows(6), 0)
Else
    MsgBox "No any ""Row"" string could be found in """ & Sheet1.Rows(6).Address & """ row..."
    Exit Sub 'the sub is exited here
             'you ca use here (instead of exiting) another Match for a different
             'string or range, if this helps somehow...
End If
相关问题