Insert lines and fill via VBA macro

kasbac

Active Member
Joined
Jan 2, 2008
Messages
344
Hi there

I am hoping for your help in creating a macro that will automate a repetitive piece of work I am doing

I have a sheet with data in column A and B, both having headers. Column A contains a name and be B has a reference to an account. Some have references to multiple accounts and are divided be a "/":

Here is an example:

Name Account
xxx xxx
yyy xxx/yyy
zzz xxx/yyy/zzz

I would like help with creating a macro that will:

A) Every time a name has multiple account references create a new line below
B) In the new line(s) fill in the name from above cell
C) Fill in one account name from the above line.

The end result based on the above inputs should look as follows:

Name Account
xxx xxx
yyy xxx
yyy yyy
zzz xxx
zzz yyy
zzz zzz

Can anyone help out with this?

If any clarification is needed please dont hesitate to reach out!

All help is highly appreciated!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about
Code:
Sub kasbac()
   Dim i As Long
   Dim Sp As Variant
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      If InStr(Cells(i, 2), "/") Then
         Sp = Split(Cells(i, 2), "/")
         Rows(i + 1).Resize(UBound(Sp)).Insert
         Cells(i, 1).Resize(UBound(Sp) + 1).Filldown
         Cells(i, 2).Resize(UBound(Sp) + 1).Value = Application.Transpose(Sp)
      End If
   Next i
End Sub
 
Upvote 0
Here is another macro that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitAccountsDown()
  Dim LastRow As Long, Individuals As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Individuals = Split(Join(Application.Transpose(Evaluate(Replace("IF({1},A2:A#&""!""&SUBSTITUTE(B2:B#,""/"",""|""&A2:A#&""!""))", "#", LastRow))), "|"), "|")
  With Range("A2:A" & 2 + UBound(Individuals))
    .Value = Application.Transpose(Individuals)
    Application.DisplayAlerts = False
    .TextToColumns , xlDelimited, , , False, False, False, False, True, "!"
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
That worked like a charm Fluff!

Thank you VERY much for a prompt solution, always a pleasure with you sharp folks in here!
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top