Converting data extract to individual line items

dan_adelais

New Member
Joined
May 15, 2017
Messages
12
Hi,

I'm trying to build a macro with the following data...

1626717957763.png


What I want to do is to transform the above into this:

1626718040665.png



Basically, what I'm trying to accomplish is to take the data set and transform it into individual line items. Account numbers, names, and risk types repeat, but LOB and Net Loss are unique. I'm at a loss - any help is much appreciated.

Thank you!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hello,

I posted a thread yesterday but I've refined what I'm looking for. Below is my data extract.

1626811131057.png


Account numbers, names, and Risk Types will not change. Therefore, I created a table of static data to start with.

1626811233600.png


What I need to do is to write a script that will create one row per account number per LOB, and provide the net loss number per that LOB. Example below. Note that there are hidden columns within the data extract by default, but the 10 shown are the only ones I need.

1626811548697.png


Any help is very much appreciated.
 
Upvote 0
Row numbers are not visible in the image.
Could you comment in which row the headers are "LOB1 LOB2 ..."
And in which row is the first "Risk type"

Note:
In the future, to help you in a more practical way and to be able to reproduce your example in our excel sheet, if it is possible for you, put the examples here using the XL2BB tool minisheets.

___
 
Upvote 0
Assuming the headers are in row 4 and the data starts in row 6.
Also assuming the account and account name are separated by a hyphen.
1626828061818.png


Try this, Data on sheet1, results on sheet2.

VBA Code:
Sub Transform_Data()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim col As Variant
  Dim i As Long, j As Long, k As Long
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Sheet1")                                          'Origen Sheet
  Set sh2 = Sheets("Sheet2")                                          'Destination Sheet
  col = Array("C", "G", "Q", "V", "Z", "AC", "AF", "AG", "AO", "AP")  'columns
  
  k = 2
  For i = 6 To sh1.Range("A" & Rows.Count).End(3).Row Step 3
    For j = 0 To UBound(col)
      sh2.Range("A" & k).Value = Split(sh1.Range("A" & i + 1), "-")(1)
      sh2.Range("B" & k).Value = Split(sh1.Range("A" & i + 1), "-")(0)
      sh2.Range("C" & k).Value = sh1.Range(col(j) & 4)
      sh2.Range("D" & k).Value = sh1.Range(col(j) & i + 1)
      sh2.Range("E" & k).Value = sh1.Range("A" & i)
      k = k + 1
    Next
  Next
End Sub
 
Upvote 0
I merged your two threads together.

In future, please do not post questions of the same nature multiple times. Clarifications/refinements to a question should be posted in a follow-up response to the original question. Per Forum Rules (#12), posts of a duplicate nature will typically be locked or deleted.

Note that Dante's responses in this thread were in relation to your original question.
 
Upvote 0
Assuming the headers are in row 4 and the data starts in row 6.
Also assuming the account and account name are separated by a hyphen.
View attachment 43185

Try this, Data on sheet1, results on sheet2.

VBA Code:
Sub Transform_Data()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim col As Variant
  Dim i As Long, j As Long, k As Long
 
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Sheet1")                                          'Origen Sheet
  Set sh2 = Sheets("Sheet2")                                          'Destination Sheet
  col = Array("C", "G", "Q", "V", "Z", "AC", "AF", "AG", "AO", "AP")  'columns
 
  k = 2
  For i = 6 To sh1.Range("A" & Rows.Count).End(3).Row Step 3
    For j = 0 To UBound(col)
      sh2.Range("A" & k).Value = Split(sh1.Range("A" & i + 1), "-")(1)
      sh2.Range("B" & k).Value = Split(sh1.Range("A" & i + 1), "-")(0)
      sh2.Range("C" & k).Value = sh1.Range(col(j) & 4)
      sh2.Range("D" & k).Value = sh1.Range(col(j) & i + 1)
      sh2.Range("E" & k).Value = sh1.Range("A" & i)
      k = k + 1
    Next
  Next
End Sub

Joe,

My apologies.

Dante,

Thank you so much. The code works partially. My headers are on row 5 and my data goes through row 77. The code stops at row 32 for some reason. Row 33 has no unique attributes. I am working on figuring out why. Does my above post, starting with that static list, make more sense? Also, there are a couple instances of account names themselves containing a hyphen. The account number is always the last 5 characters of the string.
 
Upvote 0
The macro will not work, because in the image of post #1 I asked you if they came in pairs. You actually didn't answer.

Row numbers are not visible in the images.

You could put your examples using Xl2BB tool misheet.
 
Upvote 0
The macro will not work, because in the image of post #1 I asked you if they came in pairs. You actually didn't answer.

Row numbers are not visible in the images.

You could put your examples using Xl2BB tool misheet.
I'm sorry if I did not understand your question. My company will not allow me to download add-ins unfortunately.

Are you asking if Risk Type and Account Name always have a one to one relationship? No. Many account names can be associated with one risk type. Example below. Is this what you are asking? Apologies if I am misunderstanding.

1626892517804.png
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
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