Solved

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

Posted on 2004-04-20
6
481 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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