0

Can anyone help me? (I am new in VBA and i also got help to create this macro)

I created a macro for a file and first it was working fine, but today I've been opening and restarting the file and macro hundreds of times and I'm always getting the following error: Excel VBA Run-time error '13' Type mismatch

I didn't change anything in the macro and don't know why am I getting the error. Furthermore it takes ages to update the macro every time I put it running (the macro has to run about 700 rows).

The error is in the between ** **.

VBA:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim j As Integer
Dim z(8) As Integer

Set ws = ThisWorkbook.ActiveSheet

For i = 6 To ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 3
    If Not ws.Rows(i).Hidden = True Then
        For j = 0 To 8
            If Not ws.Cells(i, j + 5) = "" Then
               ** z(j) = z(j) + ws.Cells(i, j + 5) **
            End If
        Next j
    End If
Next i

Application.EnableEvents = False
For j = 0 To 8
    ws.Cells(ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 2, j + 5) = z(j)
Next j
Application.EnableEvents = True
End Sub

2 Answers 2

2

ws.Cells(i, j + 5) at a particular point does not contain a value that can be added to z(j). It's probably blank or doesn't contain a number.

One fix would be to write

On Error Resume Next 'switch off error handling
z(j) = z(j) + ws.Cells(i, j + 5)
On Error Goto 0 'the idiomatic way of switching the default error handling back on

Effectively then, your macro will not attempt to add a non-numeric value to z(j).

But solving the problem this way is a bit like using a sledgehammer to crack a nut: perhaps it's worth investigating the contents of the cell that's causing the error, and programming around that more elegantly.

Also, I question why this needs to be in VBA in the first place. Really you're doing something that excel can do in normal calculation. (Consider SUMIF and SUMIFS) Failing that, it would be quicker if you used a VBA Function rather than responding to change events.

Sign up to request clarification or add additional context in comments.

5 Comments

VBA custom formulas are easier to maintain.
Workbooks with macros are harder to maintain.
No, formulas can reach a level of complexity that are slightly less complex than the rules for dragon poker. it is MUCH easier to maintain a simple custom formula, than a nested if formula, seven statements deep.
@RichardU: Indeed. But introducing VBA into a worksheet requires careful consideration. I'm not convinced that is the case here, and complex worksheet formulas can often be broken up. For some reason, many folk dislike having cells dedicated to holding intermediate results.
VBA is not a plague, complex formulas however, can be. I've maintained both. VBA is explicit, and if something breaks, you only need to fix it in one place.
1

In addition to what @Bathsheba mentioned, you might also want to consider declaring variables before using them to avoid problems. I wasn't able to run your code because Excel wouldn't know how to handle Z() and thought of it as a UDF. Try the following and let me know if this solves the problem:

Option Base 0
Option Explicit

Sub tmpTest()

Dim ws As Worksheet
Dim i As Long, j As Integer
Dim z(0 To 8) As Double

Set ws = ThisWorkbook.ActiveSheet

For i = 6 To ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 3
    If Not ws.Rows(i).Hidden = True Then
        For j = 0 To 8
            If Not ws.Cells(i, j + 5) = "" Then
               z(j) = z(j) + IIf(VarType(ws.Cells(i, j + 5).Value) = vbError, 0, ws.Cells(i, j + 5).Value)
            End If
        Next j
    End If
Next i

Application.EnableEvents = False
For j = 0 To 8
    ws.Cells(ws.Cells.SpecialCells(xlCellTypeLastCell).Row - 2, j + 5) = z(j)
Next j
Application.EnableEvents = True

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.