VBA Help - Transform database by adding rows when conditions are met

Yayo

New Member
Joined
Apr 12, 2023
Messages
5
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello Everyone, hope you're doing well!

First of all, this forum has been a great help as a resource to solve different excel issues and questions that I always have. As a lurker, I wanted to thank you all for the content and willingness to help!

As for my current problem/question, I wanted to make a macro that grabs and transforms some data from a database if certain conditions are met using VBA. Sadly, I haven't been able to achieve this! Hope you can help me!

Here's an example of what my database looks like and what the ideal result would be

Base Example

Data Base Transform - Example.xlsx
ABCDEF
3C1C2C3C4C5C6
4Name1X1XA123Prod110000
5Name2X2XB123Prod212000
6Name3X3XC123Prod31300200
7Name4X4XD123Prod11000200
8Name4X4XD123Prod41500200
9Name4X4XD123Prod31300200
10Name5X5XE123Prod110000
11Name6X6XF123Prod110000
12Name7X7XG123Prod9800200
13Name7X7XG123Prod10900200
14Name8X8XH123Prod71050200
DB


Desired Result Example

Data Base Transform - Example.xlsx
ABCDEF
3C1C3NewC7C2C4NewC8
4Name1A123EX1X
5BProd11000
6Name2B123EX2X
7BProd21200
8Name3C123EX3X
9BProd31300
10BC6Prod200
11Nam4D123EX4X
12BProd11000
13BProd41500
14BProd31300
15BC6Prod600
16Name5E123EX5X
17BProd11000
18Name6F123EX6X
19BProd11000
20Name7G123EX7X
21BProd9800
22BProd10900
23BC6Prod400
24Name8H123EX8X
25BProd71050
26BC6Prod200
Result


As for the conditions, here's the list of them
-> Every line will be transformed in at least 2 rows. The first one will always have a value of E (NEW C7) and data from C1, C2 and C3. The next rows will always have a value of B (NEW C7)
-> What determines the number of rows are the number of items from C4 (one for each ProdX) and if C6 contains any value (>0), the resulting item name will always be C6Prod
-> NewC8 will consolidate the values of C5 for each C4 ProdName and, in case there's a C6 value, the added sum of C6 column should be the value of C6Prod

The base will always be the same size and will have the same order, so I wanted a button that transform the DB and gives the shown result in a different sheet

Don't actually care about format, added it just for visual aid!

Thanks for your help and please let me know if something's missing

Cheers, Yayo
 
Thanks a lot @kevin9999

you've gone beyond what I expected and saved me tons of time!

Hope you have a wonderful weekend

Kind regards!
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,822
Messages
6,181,165
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