简体   繁体   中英

“Type Mismatch” error when executing Excel Macro

I have created a Macro to remove all borders from empty rows in my excel file. this is mig code:

Sub RemoveRows()
'
' RemoveRows Macro
'

'
    Range("A8").Select

    Dim checkval

    Dim RowAmount
    RowAmount = 93



    Do
        checkval = ActiveCell.Value
        If (checkval = "" Or checkval = Null) Then
            ActiveCell.EntireRow.Delete
        Else
            ActiveCell.Offset(1, 0).Select
        End If
        RowAmount = RowAmount - 1
    Loop While RowAmount > 0


End Sub

method for running the macro:

public void RemoveRows_Macro(string fileName)
{
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    xlWorkBook = xlApp.Workbooks.Open(fileName);
    xlApp.DisplayAlerts = true;

    //Run the macro
    xlApp.Run("RemoveRows", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    xlWorkBook.Save();

    xlWorkBook.Close(false);
    xlApp.Quit();
    releaseObject(xlApp);
    releaseObject(xlWorkBook);
}

My problem is that i can run this macro from my C# application without getting this error, but when others use my C# application they get this error: Run-time error '13': Type Mismatch

what am i doing wrong ?

To stay away from cell formatting always use Value2 .

Always dimension the variables type, and use them when needed only, checkval isnt very usefull, you can directly check for ActiveCell.Value2 = "" or check IsEmpty(ActiveCell) .

If you really want to use this variable than dimension it as Variant .

Excel cell values are not NULL, they are at least empty or equal to an empty string (""), so doing the IsNull or = NULL check dont make sense in VBA. Skip the check for Null and it should work fine.

Try this version

It deals with

  • possible entries that apear as empty cells but arn't (deletes them)
  • Formula's that return "" (leaves them)
  • greatly improves run time

Sub RemoveRows()
    Dim dat As Variant
    Dim i As Long
    Dim rng As Range, rngDel As Range
    Dim str As String

    Set rng = Range("A8:A100")
    dat = rng.Formula

    For i = 1 To UBound(dat, 1)
        str = Replace$(dat(i, 1), Chr(160), "")
        str = Replace$(str, vbCr, "")
        str = Replace$(str, vbLf, "")
        str = Replace$(str, vbTab, "")
        If Len(Trim$(str)) = 0 Then
            If rngDel Is Nothing Then
                Set rngDel = rng.Cells(i, 1)
            Else
                Set rngDel = Application.Union(rngDel, rng.Cells(i, 1))
            End If
        End If
    Next

    If Not rngDel Is Nothing Then
        rngDel.EntireRow.Delete
    End If

End Sub

A couple of things to try:

  1. It is good practice to declare your variable types, in this case string or variant or range .
  2. Try to use the IsNull(checkval) function instead of checkval = Null .
    • As @Chris states, a cell is indeed Empty not null , use isempty(a) .
    • After some diding into this, what seems to be a common issue, I've come across a great solution that seems fool proof, use len(trim(a)) = 0 as the check.
  3. It is unnecessary to cycle through the cells by selecting them:

Code:

Sub RemoveRows() 
'
' RemoveRows Macro 
'

' 

    Dim checkval as long, RowAmount as long
    checkval = 8
    RowAmount = 93

    Do 

        'If trim(range("A" & checkval)) = "" Or _
        '       isempty(range("A" & checkval)) Then 'thanks chris
        If Len(Trim(range("A" & checkval))) = 0 then 
            range("A" & checkval).EntireRow.Delete 
        Else 
            checkval  = checkval + 1
        End if
        RowAmount = RowAmount - 1 

    Loop While RowAmount > 0

End Sub

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