Solved

Importing Excel “cross-tab” data and extracting field values from column names

Posted on 2004-04-20
6
482 Views
Last Modified: 2008-02-01
Hi,

I am looking for a way to import data from an excel spreadsheet that is in “cross-tab” format, with locations in the first column, and item numbers as column headings. I then want to append those records to a history log. The data that’s coming in excel format looks like this:

Loc #     ITM-101     ITM-102     ITM-105     ITM-167     ITM-987
----------------------------------------------------------------------------------
LC-01        51               20               0              32             5
LC-02        20                 2              65              8              0
LC-03          0                 9               2               0            26
LC-16          5                 0              23             10             0
LC-20          0               26               0              22            85

The history log will end up storing the data like this:

Loc #       Item #       Quantity
-------------------------------------------
LC-01     ITM-100     51
LC-01     ITM-102     20
LC-01     ITM-167     32
LC-01     ITM-987       5
LC-02     ITM-101     20
LC-02     ITM-102       2
LC-02     ITM-105     65
LC-02     ITM-167       8
LC-03     ITM-102       9
LC-03     ITM-105       2
LC-03     ITM-987     26

Etc…

This will be a monthly import, so I’ll have a date column as well, but that will be user supplied through a form. The challenge is that the item numbers across the top of the spreadsheet will change from month to month and there can be anywhere between 5 and 20 of them. I’m unsure how to even begin structuring a procedure for doing this. Any/all help would be really appreciated.

Thanks!

CCorrea
0
Comment
Question by:CCorrea01
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 10870438
Greetings CCorrea01!

Your crosstab query will give you your data in the layout you exampled first.

A table is limited to 255 fields, if your number of Part designations (total, not just monthly) exceed that you will need more than one table
If that is the case you may need to resort to a coded routine that will read the crosstab and distribute the data as you need across a few tables
If not, you can build a single table with all the partnumbers.

Note:  The dashes in the Item IDs,...bad for field names.  Not deadly, but you'll be coding around those forever.

regards
jack
0
 
LVL 10

Accepted Solution

by:
perkc earned 500 total points
ID: 10871611
CCorrea,

Try the following code:

Dim xlApp As Object
Dim Sheet As Object
Dim mcol As Double
Dim vcol As Double
Dim mrow As Double
Dim vrow As Double
Dim rst As New ADODB.Recordset


Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open FileName:="C:\data\projects\temp\crossfile.xls" 'Change this file path/name
Set Sheet = xlApp.ActiveWorkbook.Sheets(1) 'Change this if the sheet to inport isn't sheet1

'Get column count
mcol = 1
Do While Sheet.Cells(1, mcol) <> ""
    mcol = mcol + 1
Loop

'Get row count
mrow = 1
Do While Sheet.Cells(mrow, 1) <> ""
    mrow = mrow + 1
Loop

If mrow = 1 Or mcol = 1 Then
MsgBox "No Data"
Exit Sub
End If

rst.CursorLocation = adUseClient
rst.Open "Select * From tblHistory", CurrentProject.Connection, adOpenForwardOnly, adLockOptimistic

vcol = 2

Do While vcol < mcol
    vrow = 2
    Do While vrow < mrow
    rst.AddNew
    rst("LocNum") = Sheet.Cells(vrow, 1)
    rst("ItemNum") = Sheet.Cells(1, vcol)
    rst("Qty") = Sheet.Cells(vrow, vcol)
    rst.Update
    vrow = vrow + 1
    Loop
    vcol = vcol + 1
Loop

MsgBox "Update complete"
rst.Close
Set Sheet = Nothing
xlApp.Quit
Set xlApp = Nothing


perkc
0
 
LVL 10

Expert Comment

by:perkc
ID: 10871801
I would also recommend adding additional fields to your History table:

HistoryID (AutoNumber-PK)
HistoryDate (Date/Time) - Import Date
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CCorrea01
ID: 10871982
Jade, I did think about doing what you suggested initially, but it didn't really work out. I need the data in more of a "transactional" format because I'm integrating and comparing it with other business systems data that’s already in that format. It’s also got to account for new item numbers as they’re introduced, and to validate them against an item master.

Perc, I'm taking a look at your code right now and applying it to what I have... I actually have about 30 fields in my history table, and it does have History ID and Date. For simplicity, I gave just the data elements I needed to extract and re-format. What you posted looks like what I need to do. I’ll play with it for a day or so and then let you know.

Thanks both!

Correa
0
 

Author Comment

by:CCorrea01
ID: 10923507
Perk,

That was what I was looking for. I was able to add some additional functionality to what you supplied, like testing to see if values were not null and not zero before adding a record, formatting out some preceding characters, and looping through a set series of sheets.

The one last thing I'm trying to do is a determine how many sheets there are in a workbook and run the import for them, and I can't seem to get it to work. Any suggestions on how to accomplish this?

Thanks,

Correa
0
 
LVL 10

Expert Comment

by:perkc
ID: 10928587
Dim vcnt As Double
vcnt = 1
Do While vcnt <= ActiveWorkbook.Sheets.Count
    Set Sheet = xlApp.ActiveWorkbook.Sheets(vcnt)
'add the code here
    vcnt = vcnt + 1
Loop

That should work. The 'ActiveWorkbook.Sheets.Count' returns the nummber of sheets in the active workbook.

perkc
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question