Solved

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

Posted on 2004-04-20
6
467 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now