Create new Excel rows based on column data

后端 未结 4 771
感动是毒
感动是毒 2021-01-23 03:43

Good afternoon all,

I have an issue where I have users who have multiple bank account details. I need to try and create a new row for each employee who has more than one

相关标签:
4条回答
  • 2021-01-23 04:06

    Right click on the tab and choose "View Code"

    Paste this code in:

    Sub SplitOnAccount()
    Dim X As Long, Y As Long, EmpNo As String, UserID As String, BSB As Variant, AccNo As Variant
    Range("F1:I1") = Application.Transpose(Application.Transpose(Array(Range("A1:D1"))))
    For X = 2 To Range("A" & Rows.Count).End(xlUp).Row
        EmpNo = Range("A" & X).Text
        UserID = Range("B" & X).Text
        BSB = Split(Range("C" & X).Text, ",")
        AccNo = Split(Range("D" & X).Text, ",")
        For Y = LBound(AccNo) To UBound(AccNo)
            Range("F" & Range("F" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = EmpNo
            Range("G" & Range("G" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = UserID
            Range("H" & Range("H" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = BSB(Y)
            Range("I" & Range("I" & Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = AccNo(Y)
        Next
    Next
    End Sub
    

    Close the window to go back to excel

    Press ALT-F8

    Choose SplitOnAccount and click run.

    Note, this is going to populate the split data to rows F to I, make sure there is nothing in there. If there is post back and we can change it.

    Also format columns F - I as text before you run it or Excel will strip leading zeros off as it will interpret it as a number.

    0 讨论(0)
  • 2021-01-23 04:16

    Here is another sub that appears to perform what you are looking for.

    Sub stack_accounts()
        Dim rw As Long, b As Long
        Dim vVALs As Variant, vBSBs As Variant, vACTs As Variant
    
        With ActiveSheet   '<-define this worksheet properly!
            For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
                vVALs = .Cells(rw, 1).Resize(1, 4).Value
                vBSBs = Split(vVALs(1, 3), Chr(44))
                vACTs = Split(vVALs(1, 4), Chr(44))
                If UBound(vBSBs) = UBound(vBSBs) Then
                    For b = UBound(vBSBs) To LBound(vBSBs) Step -1
                        If b > LBound(vBSBs) Then _
                            .Rows(rw + 1).Insert
                        .Cells(rw - (b > LBound(vBSBs)), 1).Resize(1, 4) = vVALs
                        .Cells(rw - (b > LBound(vBSBs)), 3).Resize(1, 2).NumberFormat = "@"
                        .Cells(rw - (b > LBound(vBSBs)), 3) = CStr(vBSBs(b))
                        .Cells(rw - (b > LBound(vBSBs)), 4) = CStr(vACTs(b))
                    Next b
                End If
            Next rw
        End With
    End Sub
    

    I was originally only going to process the rows that had comma delimited values in columns C and D but I thought that processing all of them would allow the macro to set the Text number format and get rid of the Number as text error warnings and keep the leading zero in 041145273.

            Split accounts and add new row(s)

    0 讨论(0)
  • 2021-01-23 04:20

    You Can definitely use Power Query to transform the data to generate new rows using split column option.

    Check this article it explains the process in detail.

    1. Load Data in Power Query section of excel.
    2. Create an Index (Not required step)
    3. Use Split column function with advance options and split them into new rows.
    4. Save this result into new table for your use.

    I did it myself and it worked like a charm.

    0 讨论(0)
  • 2021-01-23 04:23

    A formula solution:

    Image shown here.

    Delimiter: Can be a real delimiter or an absolute reference to a cell containing only the delimiter.

    HelperCol: I have to use a helper column to make it work. You need to give the column letter.

    StartCol: The column letter of the first column containing data.

    SplitCol: The column letter of the column to be splitted.

    Formula1: Used to generate the formula for the first column not to be splitted. You can fill this formula down and then fill to right.

    Formula2: Used to generate the formula for the column to be splitted(only support split one column).

    Formula3: Used to generate the formula for the Helper column. (If the title of the column to be splitted contains the delimiter, you must change the first value of the helper column to 1 manually.)

    Formula1:=SUBSTITUTE(SUBSTITUTE("=LOOKUP(ROW(1:1),$J:$J,A:A)&""""","$J:$J","$"&B2&":$"&B2),"A:A",B3&":"&B3)
    
    Formula2:=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("=MID($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,"&"""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)))+1,FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)+1))-FIND(""艹"",SUBSTITUTE($M$1&LOOKUP(ROW(A1),$J:$J,F:F)&$M$1,$M$1,""艹"",ROW(A2)-LOOKUP(ROW(A1),$J:$J)))-1)&""""","$M$1",IF(ISERROR(INDIRECT(B1)),""""&B1&"""",B1)),"$J:$J","$"&B2&":$"&B2),"F:F",B4&":"&B4)
    
    Formula3:=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("=SUM(E1,LEN(B1)-LEN(SUBSTITUTE(B1,$H$1,"""")))+1","B1",B4&1),"$H$1",IF(ISERROR(INDIRECT(B1)),""""&B1&"""",B1)),"E1",B2&1)
    

    Helper must filled one row more than the data.

    How to use:

    1. Copy the formula generated by the above three formula.
    2. Use Paste Special only paste the value.
    3. Make the formula into effect.
    4. Fill the formula.

    Bug:

    Numbers will be converted to Text. Of course you can remove the &"" at the end of the formula, but blank cells will be filled with 0.

    ps. This method may by very hard to comprehend. But once you master it, it can be very useful to solve relative problems.

    0 讨论(0)
提交回复
热议问题