在Excel中编写脚本 - 根据逗号分隔列表插入新行 [英] Scripting in Excel - Insert new row based on comma-separated list

查看:270
本文介绍了在Excel中编写脚本 - 根据逗号分隔列表插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我如何为以下情况编写一个宏:



我有一堆数据,有一个单元格,一些的数据包含更多的一个项目用逗号分隔。每次这个列中都有一个逗号,我想要添加一个新的行,其中添加了与上述相同的数据,但是与当前列的前一个项目之后是什么...我知道这一点很难遵循,所以这里是一个例子:



ORIGINAL:



应该是:



所以基本上,每次在 CORRESPONDING中遇到逗号PART 列,它创建一个新行与先前的数据,但单个部分后面的逗号。

解决方案

p>作为jswolf19提到,您可以使用 SPLIT 函数将分隔的字符串转换为数组。然后,简单地迭代数组中的项目,并根据需要插入新行。



以下过程应让您开始。



我假设您的数据位于A:E列中,并使用 rng 变量进行设置。



每个OP评论修改的代码

  Sub SplitPartsRows()
Dim rng As Range
Dim r As Long
Dim arrParts()As String
Dim partNum As Long
'# #在我的例子中,我使用列A:E,列D包含对应部分##

设置rng =范围(A1:BI13876)'##根据需要修改##'

r = 2
尽管r< = rng.Rows.Count
'##在列BB(54)中用逗号分隔值,存储在数组##
arrParts = Split(rng(r,54).Value,,)
'##如果数组中有多个项目,则添加新行##
如果UBound(arrParts)> ; = 1然后'##更正了这个逻辑为0的数组
rng(r,54).Value = arrParts(0)

'##迭代数组中的项目# #
对于partNum = 1到UBound(arrParts)
'##插入一个新行##'
'##增加行计数器变量##
r = r + 1
rng.Rows(r).Insert Shift:= xlDown

'##复制##'
rng以上的行。 Rows(r).Value = rng.Rows(r - 1).Value

'##更新新行中的零件号##'
rng(r,54)。 Value = Trim(arrParts(partNum))

'##根据需要调整我们的范围变量##
设置rng = rng.Resize(rng.Rows.Count + 1,rng.Columns .Count)

下一个

结束如果
'##增加行计数器变量##
r = r + 1
循环

End Sub


I have a question as to how I would code a macro for the following scenario:

I have a bunch of data, and there is one cell that for some of this data contains more that one item separated by commas. Every time that there is a comma in this column, I want there to be a new row added with all the same data as above but with what was after the previous item for the current column... I know this must be hard to follow, so here is an example:

ORIGINAL:

SHOULD BE:

So basically, everytime this encounters a comma in the CORRESPONDING PART column, it creates a new row with the previous data but the single part after the comma.

解决方案

As jswolf19 mentions, you can use the SPLIT function to turn a delimited string in to an array. Then, simply iterate over the items in the array and insert new rows as necessary.

The procedure below should get you started.

I assume your data is in columns A:E, and set this using the rng variable. Modify that as needed.

Code revised per OP Comments

Sub SplitPartsRows()
Dim rng As Range
Dim r As Long
Dim arrParts() As String
Dim partNum As Long
'## In my example i use columns A:E, and column D contains the Corresponding Parts ##

Set rng = Range("A1:BI13876") '## Modify as needed ##'

r = 2
Do While r <= rng.Rows.Count
    '## Split the value in column BB (54) by commas, store in array ##
    arrParts = Split(rng(r, 54).Value, ",")
    '## If there's more than one item in the array, add new lines ##
    If UBound(arrParts) >= 1 Then '## corrected this logic for base 0 array
        rng(r, 54).Value = arrParts(0)

        '## Iterate over the items in the array ##
        For partNum = 1 To UBound(arrParts)
            '## Insert a new row ##'
            '## increment the row counter variable ##
            r = r + 1
            rng.Rows(r).Insert Shift:=xlDown

            '## Copy the row above ##'
            rng.Rows(r).Value = rng.Rows(r - 1).Value

            '## update the part number in the new row ##'
            rng(r, 54).Value = Trim(arrParts(partNum))

            '## resize our range variable as needed ##
            Set rng = rng.Resize(rng.Rows.Count + 1, rng.Columns.Count)

        Next

    End If
'## increment the row counter variable ##
r = r + 1
Loop

End Sub

这篇关于在Excel中编写脚本 - 根据逗号分隔列表插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆