简体   繁体   English

根据行的单元格数据在excel中插入复制的行,然后根据行引用单元格的部分填充单元格

[英]Insert copied row in excel based on row's cell data, then populate cells based on parts of row's referenced cell

Can anyone make sense of what I am trying to do? 谁能理解我要做什么? This is the best way I can think to explain it in words. 这是我想用语言解释的最好方法。 Insert row in excel based on cell data, then copy previous row data to the new row and then populate cell from parts of the original row's referenced cell data. 根据单元格数据在excel中插入行,然后将先前的行数据复制到新行,然后从原始行引用的单元格数据的一部分中填充单元格。

Here is an image before: 这是以前的图像:

之前

And here is an image of the result: 这是结果的图像:

之后

I don't know if this can be done with VBA/Macro, a built-in formula etc. 我不知道是否可以使用VBA /宏,内置公式等完成此操作

Another way to describe my manual process is I start with row 2, if there is a value in C2 then I insert a new row for however many values are in C2 separated by a comma. 另一种描述我的手动过程的方法是从第2行开始,如果C2中有值,那么我插入一个新行,但是在C2中用逗号分隔的多个值。 I then copy contents from A2 & B2 into the new blank rows 3 & 4, then I separate the values from C2 and paste them into C2 C3, C4. 然后,我将内容从A2和B2复制到新的空白行3和4,然后将值从C2中分离出来并将其粘贴到C2 C3,C4中。 If a row doesn't have a value in the C column then I move onto the next row and so on until I reach a row in C column that has a cell value and repeat the method from above. 如果一行在C列中没有值,那么我将移动到下一行,依此类推,直到我到达C列中具有单元格值的行并从上面重复该方法。

In your workbook, create a new worksheet called " Destination ". 在您的工作簿中,创建一个名为“ Destination ”的新工作表。 Copy and paste the below code into a new module within VBA ... 将以下代码复制并粘贴到VBA中的新模块中...

Public Sub SplitRowsBasedOnLastColumn()
    Dim rngCells As Range, lngRow As Long, lngCol As Long, strLastColValue As String, i As Long
    Dim strDelimiter As String, objDestSheet As Worksheet, lngWriteRow As Long, arrValues

    Set rngCells = Selection
    strDelimiter = ","

    Set objDestSheet = Sheets("Destination")
    lngWriteRow = 1

    With rngCells
        objDestSheet.Cells.Clear

        For lngCol = 1 To .Columns.Count
            objDestSheet.Cells(1, lngCol) = .Cells(1, lngCol)
        Next

        For lngRow = 2 To .Rows.Count
            strLastColValue = .Cells(lngRow, .Columns.Count)

            If strLastColValue = "" Then strLastColValue = " "

            arrValues = Split(strLastColValue, strDelimiter)

            For i = 0 To UBound(arrValues)
                lngWriteRow = lngWriteRow + 1

                For lngCol = 1 To .Columns.Count - 1
                    objDestSheet.Cells(lngWriteRow, lngCol) = .Cells(lngRow, lngCol)
                Next

                objDestSheet.Cells(lngWriteRow, .Columns.Count) = Trim(arrValues(i))
            Next
        Next
    End With
End Sub

Now select your range of data including headers (as shown) and then run the macro. 现在选择您的数据范围,包括标题(如图所示),然后运行宏。

在此输入图像描述

After it's done, check the " Destination " sheet and you should have your result. 完成后,检查“ 目的地 ”表,您应该得到结果。

I wrote this based off the data set you provided, it may need tweaks if you have any scenarios outside of that. 我根据您提供的数据集编写了这个,如果您有任何其他情况,可能需要调整。

Let me know how it goes. 让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM