|
|
Question : How can I turn horizontal row data in Excel into vertical data like in a database using VBA
|
|
Hello, I am attempting to take a traditional Excel data format and convert it into vertical data format in Excel. My goal is to eventually move the data to Access or some other DB. For now, what I would like to do is take this type of data: WW Product Region Name 2/5/2007 2/12/2007 2/19/2007 2/26/2007 WW08 A NA demand 0 0 0 0 WW08 A EU demand 0 0 0 383 WW08 B LA demand 0 0 0 0 WW08 B NA demand 0 3116 0 0 WW08 B EU demand 0 0 0 0
And turn it into this:
Date Product Region Name Quantity WW 2/5/2007 A NA Demand 0 WW08 2/12/2007 A NA Demand 0 WW08 2/19/2007 A NA Demand 0 WW08 2/26/2007 A NA Demand 0 WW08 3/5/2007 A NA Demand 0 WW08 2/5/2007 A EU Demand 0 WW08 2/12/2007 A EU Demand 0 WW08 2/19/2007 A EU Demand 0 WW08 2/26/2007 A EU Demand 383 WW08
I am doing it in VBA and I realize that a simple transpose will not work. What I thought might do the trick is to use a loop so that for each date, I could take each row at a time and write out the data vertically. I have the horizontal data in one sheet and am moving it to another. If there is away, I am open to moving it directly from Excel (horizontal view) into Access into a table. Otherwise, I can try and create the "table" format in Ezxcel first, then move that formatted data into an Access table via append or something..
There are ofcourse many other products and more dates - this is just a sample. Here is what I have so far - it is nothing at all, but here is where I was headed:
lrow = destSht.Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row lcol = destSht.Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
Product = destSht.Cells(rw, 2) Reg = destSht.Cells(rw, 3) Name = destSht.Cells(rw, 4) WW = wkb.Sheets("About").Cells(3, 6)
col = 5 rw = 2 Do While col < lcol For each rw intermSht 'SHEET TO WRITE TO
Thanks for you help!!! This one is puzzling me, so I came back to the "exhcange" after a long hiatus....the new look is cool - much easier to use and on the eyes. Thanks again! Chris
|
Answer : How can I turn horizontal row data in Excel into vertical data like in a database using VBA
|
|
ok, import the excel file to access , name the table XXX create a table name it NewTable with fields Date Product Region Name Quantity WW
place this sub in a module
Sub CoolCatTranspose() Dim rs As DAO.Recordset, rs1 As DAO.Recordset Dim i As Integer, dfldArr, sd
Set rs = CurrentDb.OpenRecordset("xxx") Set rs1 = CurrentDb.OpenRecordset("NewTable")
If rs.EOF Or rs.BOF Then MsgBox "no records" Exit Sub End If rs.MoveFirst For i = 0 To rs.Fields.Count - 1 If IsDate(rs.Fields(i).Name) Then If sd = "" Then sd = rs.Fields(i).Name Else sd = sd & "," & rs.Fields(i).Name End If End If Next dfldArr = Split(sd, ",") Dim x
For x = LBound(dfldArr) To UBound(dfldArr) rs.MoveFirst Do Until rs.EOF With rs1 .AddNew !Date = dfldArr(x) !Product = rs("Product") !Region = rs("Region") !Name = rs("Name") !Quantity = rs.Fields(dfldArr(x)).Value !WW = rs("ww") .Update End With rs.MoveNext Loop Next End Sub
voila!!!
|
|
|
|
|