Duplicate row based on

karweng

New Member
Joined
Aug 24, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Does anyone know how I can transform my data from TableA to TableB. Either using macro or using Powerquery to perform the task?

Requirement: If there is a customer in CustomerName B col, duplicate the row and move the customer to CustomerName A col.

1636350858694.png

1636350868172.png
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
For the future, so helpers do not have to manually type out sample data to test with:
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Try this with a copy of your workbook. It is based on headers in row 2 as shown in your image.

VBA Code:
Sub Rearrange()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Range("D" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Not IsEmpty(Range("D" & r).Value) Then
      Rows(r).Copy
      Rows(r + 1).Insert
      Range("D" & r + 1).Cut Destination:=Range("C" & r + 1)
      Range("D" & r).ClearContents
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
For the future, so helpers do not have to manually type out sample data to test with:
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Try this with a copy of your workbook. It is based on headers in row 2 as shown in your image.

VBA Code:
Sub Rearrange()
  Dim r As Long
 
  Application.ScreenUpdating = False
  For r = Range("D" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Not IsEmpty(Range("D" & r).Value) Then
      Rows(r).Copy
      Rows(r + 1).Insert
      Range("D" & r + 1).Cut Destination:=Range("C" & r + 1)
      Range("D" & r).ClearContents
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
Thanks Peter, the vba code works great!!!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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