Excel错误:使用M列中的数字添加行并从N列和O列复制数据时出现编译错误13

0lvr5msh  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(80)

我正在运行这段代码,它在M列中查找产品数量,如果数量大于1,它将在该值下插入一行,并从N列向下复制数据以填充添加的单元格。
我需要它也在列L中查找数据,并做同样的事情,因为它在列N目前,如果有文本在列N我得到了一个13.13在Excel中的错误。

Sub InsertRowsAndFillDescriptions()
    Dim i As Long, j As Long, k As Long
    Dim arrData, rngData As Range, arrRes
    arrData = ActiveSheet.Range("M1").CurrentRegion.Value
    ReDim arrRes(1 To Application.Sum(Range("m:m")), 1 To UBound(arrData, 2))
    k = 1
    For i = LBound(arrData) + 1 To UBound(arrData)
        For j = 1 To arrData(i, 1)
            If j = 1 Then arrRes(k, 1) = arrData(i, 1)
            arrRes(k, 2) = arrData(i, 2)
            k = k + 1
        Next j
    Next i
    Range("M2").Resize(k - 1, 2).Value = arrRes
End Sub

字符串
这段代码不是我写的,所以我不知道如何修改它来实现这一点。我已经添加了一个图像的开始状态和结束状态的这个宏一旦完成。


的数据

hrysbysz

hrysbysz1#

  • CurrentRegion包含更多列。arrData(i, 1)不再来自Col M。
  • 更改:将列L、M、N加载到数组中。
Sub InsertRowsAndFillDescriptions()
    Dim i As Long, j As Long, k As Long
    Dim arrData, rngData As Range, arrRes, lastRow As Long
    lastRow = Cells(Rows.Count, "M").End(xlUp).Row
    arrData = Range("L1:N" & lastRow).Value
    ReDim arrRes(1 To Application.Sum(Range("M:M")), 1 To UBound(arrData, 2))
    k = 1
    For i = LBound(arrData) + 1 To UBound(arrData)
        For j = 1 To arrData(i, 2)
            If j = 1 Then arrRes(k, 2) = arrData(i, 2)
            arrRes(k, 1) = arrData(i, 1)
            arrRes(k, 3) = arrData(i, 3)
            k = k + 1
        Next j
    Next i
    Range("L2").Resize(k - 1, 3).Value = arrRes
End Sub

字符串

相关问题