I have adapted a VB macro to insert extra rows below values (see "interval" in column A) when there are duplicate instances of associated values occur (see "count" in Column C). More specifically, when there is a count of duplicates that is greater than 1 (0 = no instances, 1 = single instance) with a particular time interval, I want to insert extra rows so that there will be room to insert the duplicated values in order of their appearance.
Images of the Excel data before execution , after execution .
Below is the code I have been working with, and it appears to be working. But, it draws an error code every time I run the macro. Could someone please explain why this section of code is triggering the error, and how to fix it? here is the error code that appears: "Run-time error code '13' Type: mismatch", and debugging points to this line of code: temp = Range("B" & n)
Sub dupeIntervals()
' insert new rows to match how many utterances are in a particular time interval
Worksheets("Align").Activate
Dim r, count As Range
Dim LastRow As Long
Dim temp As Integer
Set r = Range("A:C")
Set count = Range("B:B")
LastRow = Range("B" & Rows.count).End(xlUp).Row
For n = LastRow To 1 Step -1
temp = Range("B" & n) ' << run-time error 13 / "type mismatch"
If (temp > 1) Then
Rows(n + 1 & ":" & n + temp - 1).Insert Shift:=xlDown
End If
Next n
End Sub
Thanks!
PS here is the anecdotal explanation of how I am using this macro: I am transforming utterances from transcribed interviews from continuous data (millisecond time-stamp of when utterance began) into 5-second time intervals. on occasion, there will be more than one utterance per 5s interval, which requires extra rows for inserting those extra utterances within a given interval.
Your loop includes row 1:
LastRow To 1 Step -1
Make that To 2 Step -1
to skip the heading.
That said...
You have declared temp
as an Integer
.
Dim temp As Integer
That means whatever you assign to temp
must be compatible with a 16-bit signed integer value, ie any numeric value between -32677 and 32767.
Some values will implicitly convert to an Integer
. eg 44.634 will assign temp
with 45.
Some values won't convert though.
""
(or any non-numeric string for that matter) can't be converted to an Integer
. #NA
) can't be converted to anything . Your code is making assumptions:
temp = Range("B" & n) ' << run-time error 13 / "type mismatch"
It's assuming that the ActiveSheet
is the sheet you want to be working with. It's assuming n
has a value that represents a valid worksheet row number - you haven't declared n
, so it's likely a Variant/Long
, or Variant/Integer
, so n
is probably fine (still, it should be declared).
It's assuming that the value of the range can be coerced into an Integer
.
Remove these assumptions.
Dim cellValue As Variant
cellValue = ActiveSheet.Range("B" & n).Value
If IsNumeric(cellValue) Then
temp = CInt(cellValue)
Else
'skip. cell is invalid.
Debug.Print "Row# " & n & " contains an invalid value."
End If
I'd recommend using a 32-bit integer type (ie Long
) instead of Integer
.
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.