Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-20
6
Medium Priority
?
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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