Results 1 to 6 of 6

Thread: Excel VBA Run-time error '13' Type mismatch

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    [ SOLVED ]Excel VBA Run-time error '13' Type mismatch

    Hi I have a macro that can delete rows, I have a problem with my macro because sometimes when I used it I get a Run-time error '13' Type mismatch but sometimes it is working. I don't know why sometimes I get this error, besides I am using almost the same template everytime. It works in some template, and sometimes it doesnt work, Here is my code below. I hope you guys can help me. thank you so much.

    Code:
    
    Option Explicit
    
    Sub Delete_Row_New()
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim myStrings As Variant
    Dim FoundCell As Range
    Dim i As Long
    Dim ws As Worksheet
    Dim strToDelete As String
    Dim DeletedRows As Long
    Dim c As Range
    Dim fa As String
    
        'for speed purpose
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
            
        'back to normal view, do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        'Turn off Page Breaks, do this for speed
        ActiveSheet.DisplayPageBreaks = False
        
        'search strings here
        strToDelete = Application.InputBox("Enter value to delete", "Delete Rows", Type:=2)
        If strToDelete = "False" Or Len(strToDelete) = 0 Then
            ActiveWindow.View = ViewMode
            With Application
                .ScreenUpdating = True
                .Calculation = CalcMode
            End With
            Exit Sub
        End If
        
        'make search strings array for more than one
        myStrings = Split(strToDelete)
        
        'Loop through selected sheets
        For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
            
            'search the values in MyRng
            For i = LBound(myStrings) To UBound(myStrings)
                Set c = ws.UsedRange.Find(What:=myStrings(i), _
                                                      LookIn:=xlFormulas, _
                                                      LookAt:=xlWhole, _
                                                      SearchOrder:=xlByRows, _
                                                      SearchDirection:=xlNext, _
                                                      MatchCase:=False)
                Set FoundCell = Nothing
                If Not c Is Nothing Then
                    fa = c.Address
                    Do 'Make the loop
                        If FoundCell Is Nothing Then
                            Set FoundCell = c
                        Else
                            Set FoundCell = Union(FoundCell, c)
                        End If
                        DeletedRows = DeletedRows + 1   'Count deleted rows
                        'search the used cell/range in entire sheet
                        Set c = ws.UsedRange.FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> fa
                End If
                
                If Not FoundCell Is Nothing Then
                If MsgBox("Would you like to delete (" & FoundCell.Count & ") rows of " & FoundCell & " in " & ws.Name & " tab?", vbQuestion + vbYesNo) = vbYes Then
                    FoundCell.EntireRow.Delete
                Else
                    GoTo 1
                End If
                End If
                
            Next i
            
        Next ws
        
        If DeletedRows Then
            MsgBox "Total number of deleted rows: " & DeletedRows, vbInformation, "Delete Rows Complete"
        Else
            MsgBox "No Match Found!", vbInformation, "Error Occured"
        End If
    1:
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
        
    End Sub
    The Error comes in this line

    Code:
    If MsgBox("Would you like to delete (" & FoundCell.Count & ") rows of " & FoundCell & " in " & ws.Name & " tab?", vbQuestion + vbYesNo) = vbYes Then
    Last edited by mackypogi; 09-17-2013 at 11:24 AM.

  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    18
    Rep Power
    0
    rows of " & FoundCell & " in

    Errror is in foundcell. if you can remove the foundcell you will not get the runtime error.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0
    Quote Originally Posted by aju.thomas View Post
    rows of " & FoundCell & " in

    Errror is in foundcell. if you can remove the foundcell you will not get the runtime error.
    Hi aju.thomas,

    you are correct buddy, can you suggest what can I replace with FoundCell ?

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    The line should be

    Code:
    If MsgBox("Would you like to delete (" & FoundCell.Count & ") rows in " & ws.Name & " tab?", vbQuestion + vbYesNo) = vbYes Then
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    18
    Rep Power
    0
    you can add - myStrings(i)

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0
    Quote Originally Posted by aju.thomas View Post
    you can add - myStrings(i)
    Hi aju.thomas

    Thank you so much, it works fine now !

Similar Threads

  1. Replies: 6
    Last Post: 09-03-2019, 10:26 AM
  2. Run SQL In MS-Access From Excel VBA
    By bobdole22 in forum Excel Help
    Replies: 4
    Last Post: 09-12-2013, 01:35 AM
  3. Replies: 1
    Last Post: 06-18-2013, 07:46 AM
  4. Run Time error '9': Subscript out of range
    By antonio in forum Excel Help
    Replies: 4
    Last Post: 03-26-2013, 01:53 AM
  5. Get time difference in excel using vba
    By LalitPandey87 in forum Excel Help
    Replies: 1
    Last Post: 10-09-2012, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •