I'm writing a short script in VBA that prints and compares timestamps in various cells. The code is working fine, however I'm confused with the inconsistency of the "ByRef arugement type mismatch". My code is below.
Function nextrow()
With ActiveSheet
nextrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
End Function
____
Private Sub buttonclick(nr As Integer)
With ActiveSheet
.Cells(nr, 2) = Now
If nr = 2 Then Exit Sub
dur = .Cells(nr, 2) - .Cells(nr - 1, 2)
.Cells(nr - 1, 3) = dur
End With
End Sub
____
Private Sub distract2()
nr = nextrow
If nr = 2 Then Exit Sub
buttonclick nr - 1
End Sub
If you look at distract2
, you'll notice I haven't defined nr as an integer, but even so it passes through to buttonclick
without a problem.
However, when I remove -1
from after nr, VBA throws a ByRef error.
Two questions:
- Does anyone know why this happens?
- Is it better to
dim nr as Integer
or not?