Excel VBA 工作代码今天突然出现 "type mismatch" 错误

Excel VBA working code suddenly has "type mismatch" error today

下面的代码已经 运行 完美地运行了 6 个月并且没有被更改(据我所知)。今天我 运行 代码并得到 运行-time error 13 Type Mismatch。指出错误的代码行 (类型不匹配 ------> ) 请帮忙。

Sub ADULTClearAndPaste()

Dim lr As Long, lr2 As Long, r As Long
Set Sh1 = ThisWorkbook.Worksheets("Members to cut & past")
Set Sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet")

Program = 9
ATP = 10
FIFO = 7
LastName = 2
FirstName = 3
Sh2.Select
For Each cell In Sh2.Range("B1:F756")
If cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
   cell.ClearContents
End If
    Next


lr = Sh1.Cells(Rows.Count, "B").End(xlUp).Row
W = 7
For r = 2 To lr

TYPE MISMATCH -------->  If Sh1.Range("U" & r).Value = "White" Then 
    Sh2.Cells(W, 2).Value = Sh1.Cells(r, Program).Value
    Sh2.Cells(W, 3).Value = Sh1.Cells(r, ATP).Value
    Sh2.Cells(W, 4).Value = Sh1.Cells(r, FIFO).Value
    Sh2.Cells(W, 5).Value = Sh1.Cells(r, LastName).Value
    Sh2.Cells(W, 6).Value = Sh1.Cells(r, FirstName).Value
    W = W + 1
End If

Next r

sheet 名称末尾缺少 "e" 吗?

Set Sh1 = ThisWorkbook.Worksheets("Members to cut & past")

似乎是您第一次尝试使用 sheet 时出现错误

编辑:我的错,你在这里使用它没有错误:

lr = Sh1.Cells(Rows.Count, "B").End(xlUp).Row

你的代码对我有用,可能是死引用?在 VBE 的工具/参考中检查 "missing"。

我的猜测是该单元格的值有误或包含非字符串。下面的代码应该消除你的错误:

Sub ADULTClearAndPaste()

Dim lr As Long, lr2 As Long, r As Long
Dim Sh1 as Worksheet, Sh2 as Worksheet
Dim StrVal as String
Dim Program as Integer, ATP as Integer, FIFO as Integer, LastName as Integer, FirstName as Integer
Set Sh1 = ThisWorkbook.Worksheets("Members to cut & past")
Set Sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet")

Program = 9
ATP = 10
FIFO = 7
LastName = 2
FirstName = 3

For Each cell In Sh2.Range("B1:F756")
    If cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
       cell.ClearContents
    End If
Next


lr = Sh1.Cells(Rows.Count, "B").End(xlUp).Row
W = 7
For r = 2 To lr

    On Error Resume Next
    StrVal = vbNullString
    StrVal = Sh1.Range("U" & r).Value
    On Error GoTo 0 'Or implement proper error handling
    If StrVal = "White" Then 
        Sh2.Cells(W, 2).Value = Sh1.Cells(r, Program).Value
        Sh2.Cells(W, 3).Value = Sh1.Cells(r, ATP).Value
        Sh2.Cells(W, 4).Value = Sh1.Cells(r, FIFO).Value
        Sh2.Cells(W, 5).Value = Sh1.Cells(r, LastName).Value
        Sh2.Cells(W, 6).Value = Sh1.Cells(r, FirstName).Value
        W = W + 1
    End If

Next r

End Sub

以上代码应该可以消除您的错误,但不会解决问题的根本原因。下面的代码不仅会消除您的错误,还会显示一个包含任何错误行的消息框。

Sub ADULTClearAndPaste()

Dim lr As Long, lr2 As Long, r As Long
Dim Sh1 as Worksheet, Sh2 as Worksheet
Dim StrVal as String, StrOutput as String
Dim Program as Integer, ATP as Integer, FIFO as Integer, LastName as Integer, FirstName as Integer
Set Sh1 = ThisWorkbook.Worksheets("Members to cut & past")
Set Sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet")

Program = 9
ATP = 10
FIFO = 7
LastName = 2
FirstName = 3

For Each cell In Sh2.Range("B1:F756")
    If cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
       cell.ClearContents
    End If
Next


lr = Sh1.Cells(Rows.Count, "B").End(xlUp).Row
W = 7
For r = 2 To lr

    On Error Resume Next
    If IsError(Sh1.Range("U" & r).Value) Then
        'There is an error with the value. Log it for output.
        If StrOutput = vbNullString Then 
            StrOutput = "Errors encountered with the following rows: " & r
        Else
            StrOutput = StrOutput & ", " & r
        End If
    Else
        'Execute your code
        StrVal = vbNullString
        StrVal = Sh1.Range("U" & r).Value
        On Error GoTo 0 'Or implement proper error handling
        If StrVal = "White" Then 
            Sh2.Cells(W, 2).Value = Sh1.Cells(r, Program).Value
            Sh2.Cells(W, 3).Value = Sh1.Cells(r, ATP).Value
            Sh2.Cells(W, 4).Value = Sh1.Cells(r, FIFO).Value
            Sh2.Cells(W, 5).Value = Sh1.Cells(r, LastName).Value
            Sh2.Cells(W, 6).Value = Sh1.Cells(r, FirstName).Value
            W = W + 1
        End If
    End If

Next r

'Display success or error message
If StrOutput <> vbNullString Then
    MsgBox StrOutput, vbCritical
Else
    MsgBox "Done!"
End If
End Sub