I try to make convertion from TABLE1 to TABLE2
using vba
code should go here, but when i paste it, i got erro that I have to much code
so, i add link to TXT document vba code
but can't figure out how to move the YEAR data from TABLE1 B1:G1 to column B of TABLE2.
Assuming you use the Setup as shown in your example, this worked for me:
For a non-VBa Solution, you can do this with the native Worksheets-Functions like such:
Assuming you have the setup like in the picture put these Formulas in A9, B9 and C9 and drag down as suited:
For A9;
For B9:
For C9:
K, might aswell give you some hints on how I would do this based on the information I have.
I recon there's a ton of ways to achieve this...
PivotTable
-ObjectRecordset
, then looping it's records and field-names as shown in this exampleRange
in the x and y direction (like you do)QueryTable
with some basicSQL
Using a query table:
A query table is basically a link to some external data (a textfile, a database, another excel-doc).
Points i like about query tables:
For our purpose, it is largely described with two properties (there's more to it, but we keep it simple):
The
Connection
/ConnectionString
stores information on your linked data (filetype, location, etc). In our case, this is the exact Excel-File we're working on. Check out connectionstrings.com for more info on other fiole-types.Here's how your
Connection
will likely look, assuming you use anxlsm
-File:To keep things simple, paste this into one cell and read it into a string-variable with
con = YourSheet.Cells(1,1)
. Editing it in a cell is a lot more comfortable than inside VBA.the
CommandText
stores information on what data to get. We useCommandType = xlCmdSql
here, which means that ourCommandText
will be an SQL-String, closely resembling this one:Again, paste this into one cell, adjust the Excel-ranges to your Table1-Range, and read it into a string variable with
cmd = YourSheet.Cells(1,2)
.Note: creating this SQL is obviously the messy part if you got 10+years. In that case, find a way to write the SQL semi-automatically, e.g. with formulas, and then concatenate them into one string.
This gets us started. We now can create a
QueryTable
with the aforementioned properties. The final sub looks something likeThis code contains some properties you might not need, however, since this isn't much of a performance issue, we can always get rid of them later.
Works for me, let me know if you need any help.
Hope this helps.