# Need To Take Excel Workbook with x rows and y columns and make it x * y rows and 3 columns

Hello,

I have an excel work book with y data columns, each of which currently constitute a row for x records.

I want to create a database table that has 3 columns:

1.  The 1st column of each existing row
2.  The name of the existing column
3.  The value of the column

Could anyone possibly supply a VB algorithm that will open up an existing Excel workbook, and put the output in a new one?

THANKS!!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
could you please post an example of input and output?
Commented:
as above....

do you want

A    B    C    D
E    F    G    H

to become

A    E
B    F
C    G
D   H
Author Commented:
ok, maybe I didn't explain it properly.  Let me try with an example:

The input has the headers (database columns)

PORT  FIELD1   FIELD2   FIELD3  FIELD4 ...... FIELD(n)
915     10          1.5         Y          TRUE         (value)
656     15          1.9         N          FALSE        (value)
323     17          2.0         Y          TRUE          (value)

PORT  FIELD_NAME    VALUE
915     FIELD1             10
915     FIELD2             15
915     FIELD3             Y
915     FIELD4             TRUE
915     FIELD(n)          (value)
656     FIELD1             15
656     FIELD2             1.5
656     FIELD3             Y
....and so on until all rows in the input have been converted into output...so I guess it's actually x rows * (y-1) columns.  oops.  does this make sense to anyone?
Commented:
this is done in Excel VBA.  But the same is very similar using Excel Object in VB.
I'll try to fix that tomorrow but its time for bed for me....

Sub doit()

'needs srcdata as defined range name in Excel sheet covering the range of data to be treated.
'needs destin as range in destination sheet. Top left corner of destination area.

dim i as integer, j as integer
Dim nr As Integer, nc As Integer
Dim cxx As Variant
Dim DestData As Range, SrcData As Range

Set SrcData = Range("srcdata")

Set DestData = Range("destin")

nr = SrcData.Rows.Count
nc = SrcData.Columns.Count

DestData.Offset(0, 0) = "PORT"
DestData.Offset(0, 1) = "FIELD_NAME"
DestData.Offset(0, 2) = "VALUE"
nextrow = 2
For i = 2 To nr - 1
DestData(nextrow, 1).Value = SrcData(i, 1).Value

For j = 2 To nc
DestData(nextrow, 1).Value = SrcData(i, 1).Value

DestData(nextrow, 2).Value = SrcData(1, j).Value

DestData(nextrow, 3).Value = SrcData(i, j)

nextrow = nextrow + 1
Next j
Next i

End Sub

Experts Exchange Solution brought to you by