[英]Insert number of rows based on value in adjacent cell
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). 我修改了VB宏,以便在出现重复的关联值实例(请参阅C列中的“计数”)时,在值下方插入多余的行(请参见A列中的“间隔”)。 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.
更具体地说,当在特定的时间间隔内重复项的数量大于1(0 =无实例,1 =单个实例)时,我想插入额外的行,以便有空间在其中插入重复的值它们的出现顺序。
Images of the Excel data before execution , after execution . 执行前,执行 后 Excel数据的图像。
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)
这是出现的错误代码:“运行时错误代码'13'类型:不匹配”,调试指向以下代码行:
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. PS这里是我如何使用此宏的轶事说明:我正在将转录采访中的讲话从连续数据(发声开始的毫秒时间戳)转换为5秒的时间间隔。 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.
有时,每5s间隔会有一个以上的发音,这需要额外的行才能在给定的间隔内插入这些额外的发音。
Your loop includes row 1: 您的循环包括第1行:
LastRow To 1 Step -1
Make that To 2 Step -1
to skip the heading. 使
To 2 Step -1
跳过标题。
That said... 那个...
You have declared temp
as an Integer
. 您已将
temp
声明为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. 这意味着您分配给
temp
任何内容都必须与16位带符号整数值兼容,即-32677和32767之间的任何数字值。
Some values will implicitly convert to an Integer
. 一些值将隐式转换为
Integer
。 eg 44.634 will assign temp
with 45. 例如44.634将为
temp
分配45。
Some values won't convert though. 有些值不会转换。
""
(or any non-numeric string for that matter) can't be converted to an Integer
. ""
(或任何非数字字符串)不能转换为Integer
。 #NA
) can't be converted to anything . #NA
)不能转换为任何值 。 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. 假设
ActiveSheet
是您要使用的工作表。 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). 假设
n
具有代表有效工作表行号的值-您尚未声明n
,因此很可能是Variant/Long
或Variant/Integer
,因此n
可能很好(仍然应该声明)。
It's assuming that the value of the range can be coerced into an Integer
. 假定范围的值可以强制为
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
. 我建议使用32位整数类型(即
Long
)代替Integer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.