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


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?

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.

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

do you want

  A    B    C    D
  E    F    G    H

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

The input has the headers (database columns)

915     10          1.5         Y          TRUE         (value)
656     15          1.9         N          FALSE        (value)
323     17          2.0         Y          TRUE          (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 I guess it's actually x rows * (y-1) columns.  oops.  does this make sense to anyone?
Robberbaron (robr)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

'set up headers
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

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurteenAuthor Commented:
This is exactly what I needed.  The only change I needed to make was to the first loop "For i = 2 To nr - 1"  ->  "For i = 2 To nr " 

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.