excel宏为数字diff创建新行

时间:2014-11-20 08:13:38

标签: excel vba excel-vba

我需要在单个单元格中使用具有2个值的列A,并为这两个值之间的每个数字创建一行,但保持其余列与原始行中的相同。

enter image description here

这是我当前的宏。

Sub Macro1() “ 'Macro1宏 “

”     Dim CellValue As String     strcellvalue =替换(范围(“A9”)。值,“”,“”)     如果是InStr(strcellvalue,“ - ”)那么         Dim intCurrentBIN为Double         Dim intLastBIN为Double         Dim intMainRow As Integer         Dim intNewRow As Integer         intMainRow = 9         intNewRow = intMainRow + 1

    intCurrentBIN = CDbl(Left(strcellvalue, InStr(strcellvalue, "-") - 1))
    intLastBIN = CDbl(Right(strcellvalue, Len(strcellvalue) - InStr(strcellvalue, "-")))

        Do While intCurrentBIN < (intLastBIN + 1)
            Rows(intMainRow & ":" & intMainRow).Select
            Selection.Copy
            Rows(intNewRow & ":" & intNewRow).Select
            Selection.Insert Shift:=xlDown
            Range("A" & intNewRow).Select
            ActiveCell.FormulaR1C1 = intCurrentBIN
            intNewRow = intNewRow + 1
            intCurrentBIN = intCurrentBIN + 1
        Loop
    Rows(intMainRow & ":" & intMainRow).Select
    Selection.Delete
End If

End Sub

1 个答案:

答案 0 :(得分:0)

我不清楚你的问题是什么。你的代码运行正常。下面是一段代码,无需插入行即可正常工作:

Sub createRows()

Dim iStart As Integer, iEnd As Integer, iLastRow As Integer, iThreeFour As Integer, iLoop As Integer
Dim sID As String, sDesc As String, sCode As String

For iThreeFour = 3 To 4

    sID = Range("A" & iThreeFour)
    sDesc = Range("B" & iThreeFour)
    sCode = Range("C" & iThreeFour)

    iStart = Left(sID, InStr(sID, "-") - 1)
    iEnd = Right(sID, Len(sID) - InStr(sID, "-"))
    iLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row + 1

    For iLoop = iStart To iEnd

        Range("A" & iLastRow).Value = iLoop
        Range("B" & iLastRow).Value = sDesc
        Range("C" & iLastRow).Value = sCode

        iLastRow = iLastRow + 1

    Next iLoop

Next iThreeFour

End Sub