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
""
(leaves them)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:
string
or variant
or range
.IsNull(checkval)
function instead of checkval = Null
.
Empty
not null
, use isempty(a)
.len(trim(a)) = 0
as the check.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.