Create new Excel rows based on column data

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

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 bank account, with the second bank account being allocated a new row.

Employee Number     User ID         BSB             Account number 10000591            WOODSP0         306089,116879   343509,041145273 10000592            THOMSOS0        037125          317166 

I need it to look something like this:

Employee Number     User ID     BSB     Account number 10000591            WOODSP0     306089  343509 10000591            WOODSP0     116879  041145273 10000592            THOMSOS0    037125  317166 

Any thoughts? Your input is greatly appreciated!

Screenshots are here to demonstrate:

回答1:

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.



回答2:

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.



回答3:

A formula solution:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!