Need help with VBA Code

Einz

New Member
Joined
Feb 3, 2022
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
I’m a noob with Macros but that hasn’t stopped me from learning and trying to understand code.

I have a table that has various columns ranging from b thru Al for each row. I need to be able to add a row for each column since each column should have its own row.

I hope I’m making sense. I’m including a picture of what I’m dealing with.

Thanks in advance ?
 

Attachments

  • FE1A14C3-A889-41EB-B6F9-EEAA884BFF9C.jpeg
    FE1A14C3-A889-41EB-B6F9-EEAA884BFF9C.jpeg
    62.5 KB · Views: 14

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Are you after a result or are you using this to learn VBA ?
If you are after a result, then you could do this in Power Query in only a couple of steps using the PQ unpivot function.

If you are doing it to learn vba then give us
  • the sheet name your data is on,
  • the sheet name you want the output to go to
  • whether you want blank cells in your output
  • whether you want zero value cells in your output
An XL2BB or some other copyable version of some sample data would be helpful and saving us having to recreate data at our end.
 
Upvote 0
Are you after a result or are you using this to learn VBA ?
If you are after a result, then you could do this in Power Query in only a couple of steps using the PQ unpivot function.

If you are doing it to learn vba then give us
  • the sheet name your data is on,
  • the sheet name you want the output to go to
  • whether you want blank cells in your output
  • whether you want zero value cells in your output
An XL2BB or some other copyable version of some sample data would be helpful and saving us having to recreate data at our end.
I am after the result which I would be doing this on a monthly basis.

I haven't worked with Power Query and to be honest never heard of it lol but whatever method is best is really appreciated.
(Wow!!!! Just learned about Xl2bb!!!!!)

Example.xlsx
ABCDEFGHI
1Original DataDesired Data
2
3AccountDept 1Dept 2Dept 3Dept 4AccountDept 1
41000100.0025.00200.00300.001000Dept 1100.00
5200050.0075.0050.00200.001000Dept 225.00
63000200.00300.00100.0050.001000Dept 3200.00
74000300.00200.0025.0075.001000Dept 4300.00
8500075.0050.0075.00100.002000Dept 150.00
9600025.00100.00300.0025.002000Dept 275.00
102000Dept 350.00
112000Dept 4200.00
123000Dept 1200.00
133000Dept 2300.00
143000Dept 3100.00
153000Dept 450.00
164000Dept 1300.00
174000Dept 2200.00
184000Dept 325.00
194000Dept 475.00
205000Dept 175.00
215000Dept 250.00
225000Dept 375.00
235000Dept 4100.00
246000Dept 125.00
256000Dept 2100.00
266000Dept 3300.00
276000Dept 425.00
Sheet1
 
Upvote 0
I will show what it looks like using Power Query first. If you are still interested in doing it this way I can either send you the code that you can drop into Power Query or talk you through the 4 steps (after creating the table)

Power Query needs to create a table for your original data.
Are you comfortable with Excel Tables ?
Once you have it set up, to refresh the table you can just zero out your table and copy in the new data, then hit refresh on the table.


Power query results screen.

1643956431411.png


Power Query screen - 4 steps

1643956484093.png
 
Upvote 0
I will show what it looks like using Power Query first. If you are still interested in doing it this way I can either send you the code that you can drop into Power Query or talk you through the 4 steps (after creating the table)

Power Query needs to create a table for your original data.
Are you comfortable with Excel Tables ?
Once you have it set up, to refresh the table you can just zero out your table and copy in the new data, then hit refresh on the table.


Power query results screen.

View attachment 56980

Power Query screen - 4 steps

View attachment 56981
Yes definitely interested if this is something that I can manipulate if I have more columns than my example does, and yes I've dabbled a bit with tables.
 
Upvote 0
I can give you the M Code to just drop into the Power Query Advanced editor but here is the step by step instructions if you want to give it a try first.

Convert the Data Table to an Excel Table
  • Select a cell in the data range and hit Ctrl+T
    Select My Table has headers
  • Under Table Design > Give it a Table Name (box far left)
    I suggest you use the same name I have for now which is tblData
Power Query (PQ) steps
  • With a cell inside the table selected
    in Excel > Data > From Table
  • This will put you in PQ
  • In the navigation pane on the right
    • Delete the Changed Type step
    • In the name box give it a different name (the output table will use this name)
  • With the 1st column (Account) seleted.
    Transform > Unpivot Columns > Unpivot Other Columns
  • You are nearly done
  • Hit ctrl+A to select all the cells in the data grid and go
    Tranform > Detect Data Type
  • Click on the 123 of the Value field and I changed it to decimal. Click on “replace current” when it pops up.
    (if you know you will never have more than 4 decimal places you could select currency)
  • Double click on any column headings you want to change and type in the heading you want.
  • Home > Close and Load To > Select
    • Table
    • Existing worksheet
    • Navigate to where you want to put it
As long as you put the new data in the same table, you just need to hit refresh on the query.
 
Upvote 0
Solution
I can give you the M Code to just drop into the Power Query Advanced editor but here is the step by step instructions if you want to give it a try first.

Convert the Data Table to an Excel Table
  • Select a cell in the data range and hit Ctrl+T
    Select My Table has headers
  • Under Table Design > Give it a Table Name (box far left)
    I suggest you use the same name I have for now which is tblData
Power Query (PQ) steps
  • With a cell inside the table selected
    in Excel > Data > From Table
  • This will put you in PQ
  • In the navigation pane on the right
    • Delete the Changed Type step
    • In the name box give it a different name (the output table will use this name)
  • With the 1st column (Account) seleted.
    Transform > Unpivot Columns > Unpivot Other Columns
  • You are nearly done
  • Hit ctrl+A to select all the cells in the data grid and go
    Tranform > Detect Data Type
  • Click on the 123 of the Value field and I changed it to decimal. Click on “replace current” when it pops up.
    (if you know you will never have more than 4 decimal places you could select currency)
  • Double click on any column headings you want to change and type in the heading you want.
  • Home > Close and Load To > Select
    • Table
    • Existing worksheet
    • Navigate to where you want to put it
As long as you put the new data in the same table, you just need to hit refresh on the query.
This is so crazy!!!! I am in awe! Thank you very much, sir. This worked like a charm. Probably easier than going the VBA route. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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