简体   繁体   中英

Excel 2013 VBA Runtime Error 13 Type Mismatch

I'm currently working on a Excel file and added some VBA. I added a button on Sheet1 with the following VBA:

Sub AddRow()
Dim c, d As Range
Set rng = ActiveSheet.Range("A1:A100")
Set rng2 = Worksheets("Sheet2").Range("A1:A100")
For dblCounter = rng.Cells.Count To 1 Step -1
    Set c = rng(dblCounter)
   If c.Value Like "XXXXXX" Then
        c.EntireRow.Insert
        For dblCounter2 = rng2.Cells.Count To 1 Step -1
        Set d = rng2(dblCounter2)
        If d.Value Like "YYYYYY" Then
        d.EntireRow.Insert
        End If
        Next dblCounter2
    End If
Next dblCounter
End Sub

The purpose of the code is to add a new row on Sheet1 above the cell containing "XXXXXX" and a new row on Sheet2 above the cell containing "YYYYYY". Adding a new row on Sheet1 works. However, adding a new row on Sheet2 doesn't. I get a 'Runtime Error 13 Type Mismatch' error at If d.Value Like "YYYYYY" Then but no error at If c.Value Like "XXXXXX" Then and i don't know why. Thanks in advance

try to change

                If d.Value Like "YYYYYY" Then
                    d.EntireRow.Insert
                End If

to this

If Not IsError(d.Value) Then
   If d.Value Like "YYYYYY" Then d.EntireRow.Insert
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM