Solved

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

Posted on 2004-04-20
6
485 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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