类型:不匹配的VBA错误-代码不起作用

时间:2020-08-03 20:06:14

标签: excel vba type-mismatch

我在下面的代码中(在行旁边指示)不断收到类型不匹配错误,有趣的是宏正在运行,但是当我通过电子邮件将其发送给另一台计算机上的时候,它坏了。知道如何解决这个问题吗?

为提供背景信息,宏是针对一种用户表单的,该表单将问题记录到不同的选项卡中(取决于问题所在)。将问题标记为已完成后,该问题将移至“ 5.完成并验证”选项卡中,为行分配一个数字,并添加一列以说明完成此问题所花费的天数。将问题移出“源”选项卡后,将其复制,删除,然后添加到“ 5.完成并验证”选项卡中。

Option Explicit

Sub Complete()

Dim sourceWS As Worksheet
Set sourceWS = ActiveSheet
Dim destWS As Worksheet
Set destWS = ThisWorkbook.Worksheets("5. Complete & Verified")

Dim RowCountS, RowCountD As Long

Dim intLastRowSrc As Long
RowCountS = ActiveSheet.Rows.Count
intLastRowSrc = sourceWS.Cells(RowCountS, 1).End(xlUp) + 1'Type Mistmatch

Dim intLastRowSDes As Long
RowCountD = Worksheets("5. Complete & Verified").Rows.Count
intLastRowSDes = destWS.Cells(RowCountD, 2).End(xlUp) + 1 'Type Mismatch

Dim r As Long

Dim iRow2 As Long
iRow2 = Application.WorksheetFunction.CountA(Sheets("5. Complete & Verified").Range("B:B")) + 1

Dim iRow3 As Long
iRow3 = Application.WorksheetFunction.CountA(sourceWS.Range("A:A")) + 1


Dim LastRow4 As Long
Dim LastRow5 As Long


For r = intLastRowSrc To 2 Step -1
        If sourceWS.Cells(r, "R") = "Complete & Verified" Then
            intLastRowSDes = destWS.Cells(RowCountD, 2).End(xlUp) + 2
            destWS.Range("C" & intLastRowSDes & ":S" & intLastRowSDes).Value = sourceWS.Range("B" & r & ":R" & r).Value
            sourceWS.Rows(r).Delete
            destWS.Cells(intLastRowSDes, 1) = sourceWS.Name ' Adding tab name as the Payer
            destWS.Cells(iRow2, 2) = iRow2 - 1 ' Numbering the rows in order instead of copying number from payer tab
            
            'Adding in Column for Days to Complete
            Worksheets("5. Complete & Verified").Cells(2, 20) = "=IF(RC[-3]="""",0,RC[-3]-RC[-9])"
            LastRow5 = Worksheets("5. Complete & Verified").Range("A1").End(xlDown).Row 'last row filled number row number not cell value 'need to copy before pasting
            Worksheets("5. Complete & Verified").Range("T2").Copy
            Worksheets("5. Complete & Verified").Range("T2" & ":" & "T" & LastRow5).PasteSpecial (xlPasteFormulas) 'paste formulas
            Worksheets("5. Complete & Verified").Range("A1") = "Payor"
            Worksheets("5. Complete & Verified").Range("A1") = "Payor"
            Worksheets("5. Complete & Verified").Activate
            Range("A1").Select
            ActiveWindow.ScrollRow = 1
            ActiveWindow.ScrollColumn = 1

            'Renumbering the No. Column on the Payer Tab
            sourceWS.Activate
            
            If IsEmpty(sourceWS.Range("A2")) = False Then
                LastRow4 = Range("A1").End(xlDown).Row 'last row filled number row number not cell value 'need to copy before pasting
                If (LastRow4 = 2) Then
                sourceWS.Range("A2") = 1
                Else
                sourceWS.Range("A2") = "=ROW()-1"
                sourceWS.Range("A2").Copy
                sourceWS.Range("A2" & ":" & "A" & LastRow4).FillDown 'paste formula
                End If
                sourceWS.Calculate
                sourceWS.Range("A2" & ":" & "A" & LastRow4).Copy
                sourceWS.Range("A2" & ":" & "A" & LastRow4).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
                Range("A2").Select
            
    End If
    End If

Next



Exit Sub

End Sub

1 个答案:

答案 0 :(得分:0)

sourceWS.Cells(RowCountS,1).End(xlUp)返回一个范围引用,并且您不能在范围内添加数字。尝试:sourceWS.Cells(RowCountS,1).End(xlUp).Row()+ 1,您应该有一个有效的行号。